Chinmaay
Chinmaay

Reputation: 193

Parse Complex JSON in Flutter

I am parsing a complex json data into my flutter app. I have attached my code and my json data below.I am getting my data into the "newData".But getting the below error. Error: "Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Iterable". Wanted to show it in the Listview.Help is highly appereciated

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

Upvotes: 0

Views: 171

Answers (1)

nvoigt
nvoigt

Reputation: 77285

As Eldar said in the comment:

for(Map i in newData[1]){

needs to be

for(Map i in newData){

Upvotes: 1

Related Questions