我的代码面临一个问题。我知道错误:
type 'List<dynamic>' is not a subtype of type 'Iterable<Map<dynamic, dynamic>>'这是我的密码:
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);
}当我做print(response);的时候
[{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}]发布于 2022-09-19 20:23:38
这代码在我这边很好用。确保从正确的包导入groupBy方法:
import "package:collection/collection.dart";并尝试将该类型添加到您的响应中:
List<Map<dynamic, dynamic>> response =我认为这个答案解决了你的问题:Flutter/Dart how to groupBy list of maps
发布于 2022-09-19 20:52:38
用这个:
var response = res.body as List;
(response.map((e) async => await groupBy().fromMap(e))).toList()将此替换为您的模型:
// 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,
};
}https://stackoverflow.com/questions/73778462
复制相似问题