json - 在Flutter中解析复杂的JSON

标签 json flutter dart

我正在将复杂的json数据解析到我的flutter应用程序中。我在下面附加了我的代码和json数据。我将数据放入“newData”中。但是出现以下错误。
错误:“未处理的异常:类型'_InternalLinkedHashMap '不是类型'Iterable'的子类型。想要在Listview中显示它。Help非常感谢

JSON Data

  {

"Status":200,

"Message":"Success",

"data":{

    "TotalRecords":10,

    "Records":[

                 {

                 "Id":1,

                 "title":"Smile Crowdfunding",

                 "shortDescription":"This foundation will bring smile on there faces",

                 "collectedValue":500,

                 "totalValue":5000,

                 "startDate":"05/05/2018",

                 "endDate":"10/06/2018",

                 "mainImageURL":"http://iphonedeveloperguide.com/oneinr/project1.jpg"

                 },

                 {

                 "Id":2,

                 "title":"Animal Funding",

                 "shortDescription":"This foundation will help animals",

                 "collectedValue":200,

                 "totalValue":10000,

                "startDate":"10/05/2018",

                "endDate":"11/06/2018",

                 "mainImageURL":"http://iphonedeveloperguide.com/oneinr/project2.jpg"

                 }
            ]

    }

}

Code

 class _ShowListState extends State<ShowList> {
  var loading = false;
  List<Record> dataModel = [];
  Future<Null> getData() async{    
    final responseData = await http.get("https://xxxxxxx.json");    
    if(responseData.statusCode == 200){
      Map<String, dynamic> map = jsonDecode(responseData.body);
      LinkedHashMap<String,dynamic> data = map["data"];
      List<dynamic> newData = data["Records"];        
      print("newData: $newData");
      setState(() {
        for(Map i in newData[1]){
          dataModel.add(Record.fromJson(i));
          print("newData: $data");
        }
        loading = false;
      });
    }
  }
  void initState() {
    // TODO: implement initState
    super.initState();
    getData();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Machine Test'),
      ),
      body: Container(
        child: loading? Center(
          child: CircularProgressIndicator()):
        ListView.builder(
            itemCount: dataModel.length,
            itemBuilder: (context,i){
              final nDataList = dataModel[i];
              return Container(
                child: Text(nDataList.title),
              );
            } ),
        ),
      );
  }
}

最佳答案

正如Eldar在评论中所说:

for(Map i in newData[1]){
需要是
for(Map i in newData){

关于json - 在Flutter中解析复杂的JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63699981/

相关文章:

ios - 无法填充第二个 UITableView Controller

javascript - 关于如何在播放器中为下一个/上一个添加自定义控件有什么想法吗?

Flutter:预览手机图库中的一张或多张图片?

flutter - 在 flutter 中实现 Material 可扩展输入芯片的最佳方法?

ubuntu - 为什么 Dart 的 "Process.start"不能在 Ubuntu 终端执行命令时执行 Ubuntu 命令?

javascript - flutter 导航使上一个屏幕在底部可见

flutter - 在Flutter中按下按钮后更新文本

mysql - 从 MySQL 获取 JSON 数据树

Java Spring - 服务 Thymeleaf URI 和 JSON

flutter - 在Flutter Web中直接使用url参数导航到网页