在Flutter中解析复杂的JSON

时间:2020-09-02 06:36:50

标签: json flutter dart

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

JSON数据

  {

"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"

                 }
            ]

    }

}

代码

 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),
              );
            } ),
        ),
      );
  }
}

1 个答案:

答案 0 :(得分:1)

正如Eldar在评论中所说:

for(Map i in newData[1]){

需要成为

for(Map i in newData){