Home > Net >  type 'List<dynamic>' is not a subtype of type 'Iterable<Map<dynamic, dyna
type 'List<dynamic>' is not a subtype of type 'Iterable<Map<dynamic, dyna

Time:09-20

I am facing a problem with my code. I get the error :

type 'List<dynamic>' is not a subtype of type 'Iterable<Map<dynamic, dynamic>>'

this is my code :

 void getData() async {
    var url = 'http://xxxxxxx/getEvents.php';
    var res = await http.get(Uri.parse(url));
    var response = json.decode(res.body);
    print(response);

    var mySelectedEvents =
        groupBy(response, (Map obj) => obj['date']).map((k, v) => MapEntry(
            k,
            v.map((item) {
              item.remove('date');
              return item;
            }).toList()));
    print(mySelectedEvents);
  
  }

when I do print(response); I otiens

[{id: 5, date: 2022-09-17, selectdate: 2022-09-17 22:13:04.508644, descript: azerty, title: azertyui, id_event_date: 4}, {id: 6, date: 2022-09-17, selectdate: 2022-09-17 23:19:05.885897, descript: 11, title: AZE, id_event_date: 5}, {id: 7, date: 2022-09-17, selectdate: 2022-09-17 23:19:05.885897, descript: 22, title: 4556, id_event_date: 6}, {id: 8, date: 2022-09-20, selectdate: 2022-09-20 00:00:00.000Z, descript: 77, title: HHJ, id_event_date: 7}, {id: 9, date: 2022-09-17, selectdate: 2022-09-17 23:20:46.357121, descript: 44, title: BYYY, id_event_date: 8}]

CodePudding user response:

The code works fine on my side. Make sure that you import the groupBy method from the correct package:

import "package:collection/collection.dart";

And try to add the type to your response:

List<Map<dynamic, dynamic>> response =

I think this answer solves your problem: Flutter/Dart how to groupBy list of maps

CodePudding user response:

Use this:

  var response = res.body as List;
  (response.map((e) async => await groupBy().fromMap(e))).toList()
  • Related