Home > Software engineering >  The argument type 'String' can't be assigned to the parameter type 'Iterable<
The argument type 'String' can't be assigned to the parameter type 'Iterable<

Time:10-31

I am struggling to find a solution to my problem. the error that appears is

The argument type 'String' can't be assigned to the parameter type 'Iterable<Map<dynamic, dynamic>>'

my code

loadPreviousEvents() async {
    var url = 'http://xxxxxxxxxx/getEvents.php';
    var res = await http.get(Uri.parse(url));
    var response = res.body;
   var newMap = groupBy(response, (Map oj) => oj['date']);

  }

CodePudding user response:

the response got from the HTTP request is JSON, in other words, it's a String containing all data, you need to decode it to get the actual data type of it :

loadPreviousEvents() async {
var url = 'http://xxxxxxxxxx/getEvents.php';
var res = await http.get(Uri.parse(url));
var response = res.body;
var newMap = groupBy(jsonDecode(response), (Map oj) => oj['date']);
 }

CodePudding user response:

i have this error now

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

i want to use this model

   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