【发布时间】:2022-10-30 23:02:48
【问题描述】:
我正在努力寻找解决问题的方法。 出现的错误是
没有为“GroupBy”类型定义方法“fromMap”。
我的模型
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,
};
}
这是我一直试图运行的代码,但没有成功。你能帮助我吗 ?
loadPreviousEvents() async {
var url = 'http://xxxxxxx/getEvents.php';
var res = await http.get(Uri.parse(url));
var response = res.body as List;
print(response);
(response.map((e) async => await GroupBy.fromMap(e))).toList();
}
【问题讨论】:
-
您需要将
fromMap替换为fromJson
标签: flutter