0

I am reading a json with few different arrays. So I want to map it to its individual model list. Thus first I created the list for e.g. as below.

class Summary {


  final String totalDuration;
  final String totalMileage;
  //final String fleetID;

  Summary({this.totalDuration, this.totalMileage});

  Map<String, dynamic> toJson() => {
        'totalDuration': totalDuration,
        'totalMileage': totalMileage,

      };

  factory Summary.fromJson(Map<String, dynamic> json) {
    return new Summary(
      totalDuration: json['totalDuration'],
      totalMileage: json['totalMileage'],

    );
  }
}

I have managed to call my api and below is how my json results look like its not the complete one as I wanted to make it brief.

"totalSummary": 6,
    "Summary": [
        {
            "totalDuration": "2549",            
            "totalMileage": "22.898"
        },
        { 
            "totalDuration": "11775",
            "totalMileage": "196.102"
        },
        {
            "totalDuration": "17107",
            "totalMileage": "232.100"
        },
        {  
            "totalDuration": "34870",
            "totalMileage": "177.100"
        },
        {
            "totalDuration": "33391",
             "totalMileage": "168.102"
        },
        {
            "totalDuration": "13886",
            "totalMileage": "77.398"
        }
    ],
    "totalDetails": 16,
    "details": [
          ..........
        ]

Below is how I wrote to populate the summary array.

final fullJson = await NetworkUtils.post(url,token,data);
        print("fullJson"+fullJson.toString());
        try{
        Summary summary1 = new Summary.fromJson(fullJson['Summary']);

         print(summary1.totalMileage);
        }
        catch(Err){
          print("Erro is at"+Err.toString());
        }

I end up getting type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>'

1 Answer 1

2

It is because the response returned contains a List of Map instead of a map .

change your line Summary summary1 = new Summary.fromJson(fullJson['Summary']);

to Summary summary1 = new Summary.fromJson(fullJson['Summary'][i]);

where i can be any value between 0 to the numbers of objects inside Summary minus one.

To get the full list of Summary objects do this:

int totalSummaryCount = fullJson['totalSummary'];
List<Summary> list = new List<Summary>.generate(totalSummaryCount, (index)=>Summary.fromJson(fullJson['Summary'][index]));
Sign up to request clarification or add additional context in comments.

7 Comments

how to do it for the whole list at one go rather than one by one?
ok I shall try. So when I recall the api do I need to first clear the previous List<Sumarry>
What is the correct command to fully clear any of the List<Summary> etc mean after each api I dont want any old value in it at all
you can use list.clear(); when you receive response from API or you could just create a class variable and update it with obtained result. You can see official List documentation here
how to clear cause we define as List<Summary> list = new List<Summary> ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.