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

Viewed 37

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}]
2 Answers

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

Use this:

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

replace this instead of your model:

    // To parse this JSON data, do
//
//     final groupBy = groupByFromJson(jsonString);

import 'dart:convert';

class GroupBy {
    GroupBy({
        this.id,
        this.date,
        this.selectdate,
        this.descript,
        this.title,
        this.idEventDate,
    });

    final int id;
    final DateTime date;
    final DateTime selectdate;
    final String descript;
    final String title;
    final int idEventDate;

    factory GroupBy.fromRawJson(String str) => GroupBy.fromJson(json.decode(str));

    String toRawJson() => json.encode(toJson());

    factory GroupBy.fromJson(Map<String, dynamic> json) => GroupBy(
        id: json["id"] == null ? null : json["id"],
        date: json["date"] == null ? null : DateTime.parse(json["date"]),
        selectdate: json["selectdate"] == null ? null : DateTime.parse(json["selectdate"]),
        descript: json["descript"] == null ? null : json["descript"],
        title: json["title"] == null ? null : json["title"],
        idEventDate: json["id_event_date"] == null ? null : json["id_event_date"],
    );

    Map<String, dynamic> toJson() => {
        "id": id == null ? null : id,
        "date": date == null ? null : "${date.year.toString().padLeft(4, '0')}-${date.month.toString().padLeft(2, '0')}-${date.day.toString().padLeft(2, '0')}",
        "selectdate": selectdate == null ? null : "${selectdate.year.toString().padLeft(4, '0')}-${selectdate.month.toString().padLeft(2, '0')}-${selectdate.day.toString().padLeft(2, '0')}",
        "descript": descript == null ? null : descript,
        "title": title == null ? null : title,
        "id_event_date": idEventDate == null ? null : idEventDate,
    };
}
Related