0

i have this json response from an API

{
    "success": true,
    "routes": [
        {
            "_id": "613cc90623db8f3418f59f50",
            "transit": [
                "Addis Ababa",
                "Gondar"
            ],
            "startingPlace": {
                "country": "Ethiopia",
                "city": "Addis Ababa"
            },
            "destination": {
                "country": "Ethiopia",
                "city": "Addis Ababa"
            },
            "distance": 55,
            "__v": 0,
            "schedule": [
                {
                    "_id": "613e200446560f0508778adf",
                    "boardingDate": "2020-01-01T00:00:00.000Z",
                    "arrivalDate": "2020-02-01T00:00:00.000Z",
                    "driverAssistantId": "612de5d3cf56fc27e8abf629",
                    "routeId": "613cc90623db8f3418f59f50",
                    "__v": 0
                }
            ]
        }
    ]
}

i wanted to retrieve the schedule list inside routes and this is the function that does the post request.. it returns a list of Schedules model

Future<List<Schedule>> searchSchedule(SearchScheduleModel model) async {
  String token = await getToken();
  Map<String, String> headers = {
    "Content-Type": "application/json",
    'Authorization': 'Bearer $token'
  };
  final allPlacesURl = Uri.parse("$ipAddress/common/searchBySpAndDes");
  var body = searchScheduleModelToJson(model);

  var response = await http.post(allPlacesURl, headers: headers, body: body);
  if (response.statusCode == 200) {
    var body = schedulesModelFromJson(response.body);
    List<Route> route = body.routes;
    List<Schedule> schedules = [];

    //the error is shown here, how can i map a list within alist

    schedules = route.map((e) => e.schedule);

    return schedules;
  }
  return json.decode(response.body);
}

but i have this error

A value of type 'Iterable<List<Schedule>>' can't be assigned to a variable of type 'List<Schedule>'.

i dont know how to map it right..

Danny
  • 317
  • 4
  • 17
  • Go here https://app.quicktype.io/ and copy paste your `json` as it is. It will generate the classes for you. – esentis Sep 12 '21 at 19:31
  • That's what I used to generate the classes... but I don't know how to map a list.. and store the mapped list in another list.. so that I could return it. @esentis – Danny Sep 12 '21 at 19:32

1 Answers1

1

The error tells you that you are trying to assign Iterable<List> to List. I am not 100% sure because I do not see the Route model, but it looks to me that the Route has a property schedule which is of type List.

To fix it Remove this line:

schedules = route.map((e) => e.schedule);

And replace it with this line

for (var route in routes) {
 schedules.addAll(route.schedule);
}
Nazarii Kahaniak
  • 461
  • 3
  • 11
  • you have no idea how you have saved me from hours of headache.. thank you so much.. can you explain what that for loop does.. is it inserting the items from routes into the schedules list or.. – Danny Sep 12 '21 at 20:20
  • Yes, exactly. The for is iterating through the routes. For each route in the routes we are inserting all the schedules from the route.schedule into the schedules list – Nazarii Kahaniak Sep 12 '21 at 20:24
  • can you be able to help me with this? im learning about getx and i cant get it to work.. https://stackoverflow.com/questions/69170332/getx-remove-value-from-list – Danny Sep 14 '21 at 00:14