我这里有一些json对象-
{
"error": "0",
"message": "Got it!",
"data": [
{
"status": "false",
"_id": "5e3cf1deda2fb4000751d2a2",
"group_id": "5e37ff5172828bad9806386e",
"date": "2020-02-06T00:00:00.000Z",
"title": "brand",
"priority": 3,
"description": ""
},
{
"status": "false",
"_id": "5e3bc849640e57000821bd60",
"group_id": "5e37ff9872828b1ed806386f",
"date": "2020-02-05T00:00:00.000Z",
"title": "new task",
"priority": 4,
"description": ""
},
{
"status": "false",
"_id": "5e37c578a9678c2863bbb4e8",
"title": "making task with true status 1",
"date": "2020-02-03T00:00:00.000Z",
"group_id": "5df8aaae2f85481f6e31db59",
"priority": 1,
"description": "some description"
},
{
"status": "false",
"_id": "5e37c9524c05b6f1560d9d9e",
"group_id": "5e35214f89e915437c2a9705",
"date": "2020-02-02T00:00:00.000Z",
"title": " h h h ",
"priority": 4,
"description": ""
}
],
"total_count": 4,
"skip": 0
}
我正在尝试将此数据获取到日历小部件(使用此插件-https://pub.dev/packages/table_calendar)。但我一直收到此错误-
[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: Invalid argument(s)
有一个地图和一个显示UI的列表-该地图允许日历显示具有相应“ date”参数的任何json对象,并以此方式在UI中进行反映(顶部带有点的日期带有一个json对象的“日期”参数映射到“标题”参数)
API还具有一个请求正文-“年”和“月”。年和月参数必须在日历中具有当前显示的年和月。
该列表应该在列表视图中显示所有具有相同“ date”参数的json对象。我能够显示获取并将数据显示到地图,但是当我尝试将数据获取到列表时,出现上述错误。
发布请求以获取地图-
Map<DateTime, List> _events;
// inside initstate
getTask().then((val) => setState(() {
_events = val; // _events is the map
print("PRINTING EVENTS = $_events");
print("PRINTING EVENTSval = $val");
}));
returnMonth(){ // using this method since the plugin returns an integer for the currently displayed month whereas The api requires a string with the name of month.
if(_calendarController.selectedDay.month == 1){
return "january";
}else
if(_calendarController.selectedDay.month == 2){
return "february";
}else
if(_calendarController.selectedDay.month == 3){
return "march";
}else
if(_calendarController.selectedDay.month == 4){
return "april";
}else
if(_calendarController.selectedDay.month == 5){
return "may";
}else
if(_calendarController.selectedDay.month == 6){
return "june";
}else
if(_calendarController.selectedDay.month == 7){
return "july";
}else
if(_calendarController.selectedDay.month == 8){
return "august";
}else
if(_calendarController.selectedDay.month == 9){
return "september";
}else
if(_calendarController.selectedDay.month == 10){
return "october";
}else
if(_calendarController.selectedDay.month == 11){
return "november";
}else
if(_calendarController.selectedDay.month == 12){
return "december";
}
}
// outside initstate
Future<Map<DateTime, List>> getTask() async {
Map<DateTime, List> mapFetch = {};
await Future.delayed(const Duration(seconds: 1), () {});
String link = fetchTodoInCalendarUrl;
var res = await http.post(link, headers: {"Accept": "application/json"}, body: {"month": returnMonth(), "year" : jsonEncode(_calendarController.selectedDay.year)});
if (res.statusCode == 200) {
Welcome event = welcomeFromJson(res.body);
}
Welcome event =welcomeFromJson(res.body);
setState(() {
for (int i = 0; i < event.data.length; i++) {
mapFetch[event.data[i].date] = [event.data[i].title];
}
});
return mapFetch;
}
发布列表请求-
List _selectedEvents;
// inside initstate
getTaskPractice().then((val) => setState(() {
_selectedEvents = val;
print("PRINTING SELECTED EVENTS = $_selectedEvents");
print("PRINTING EVENTSval = $val");
}));
// outside initstate
Future<List> getTaskPractice() async {
List mapFetch = [];
await Future.delayed(const Duration(seconds: 1), () {});
String link = fetchTodoInCalendarUrl;
var res = await http.post(link, headers: {"Accept": "application/json", "Content-Type": "application/x-www-form-urlencoded"}, body: {"month" : returnMonth(), "year" : jsonEncode(_calendarController.selectedDay.year)});
if (res.statusCode == 200) {
var data = json.decode(res.body);
var rest = data['data'];
var error = data['error'];
print("this is error = $error");
// print("PrintingREST = $rest");
print("PRINTING REST FOR TASKPRACTICE= $rest");
print("PRINTING RESBODY FOR TASK PRACTICE= ${res.body}");
final demoJsonMapEntries = rest.map((data) {
return MapEntry(DateTime.parse(data['date']), data['title']);
});
print("PRINTING DEMOJSONMAPENTRIES = $demoJsonMapEntries");
demoJsonMapEntries.forEach((e) {
// Normalize the `date` - this is necessary to ensure proper `Map` behavior
final key = DateTime.utc(e.key.year, e.key.month, e.key.day, 12);
_events.update(key, (list) => list..add(e.value), ifAbsent: () => [e.value]);
DatumCalendar event = welcomeFromJsonDatumCalendar(res.body);
// for (int i = 0; i < res.body.length; i++) {
// mapFetch[event.date][i] = [event.title][i]; // tried to do this but since event.date is a datetime, this statement returns an error
//
// }
});
}
Welcome event = welcomeFromJson(res.body);
setState(() {
// for (int i = 0; i < event.data.length; i++) {
// mapFetch[event.data[i].date.toString()] = [event.data[i].title];
//
// }
});
return mapFetch;
}
json模型=
Welcome welcomeFromJson(String str) => Welcome.fromJson(json.decode(str));
//String welcomeToJson(Welcome data) => json.encode(data.toJson());
class Welcome {
dynamic error;
dynamic message;
List<DatumCalendar> data;
dynamic totalCount;
dynamic skip;
Welcome({
this.error,
this.message,
this.data,
this.totalCount,
this.skip,
});
factory Welcome.fromJson(Map<String, dynamic> json) => Welcome(
error: json["error"],
message: json["message"],
data: List<DatumCalendar>.from(json["data"].map((x) => DatumCalendar.fromJson(x))),
totalCount: json["total_count"],
skip: json["skip"],
);
Map<String, dynamic> toJson() => {
"error": error,
"message": message,
"data": List<dynamic>.from(data.map((x) => x.toJson())),
"total_count": totalCount,
"skip": skip,
};
}
DatumCalendar welcomeFromJsonDatumCalendar(String str) => DatumCalendar.fromJson(json.decode(str));
class DatumCalendar {
dynamic status;
dynamic id;
dynamic groupId;
DateTime date;
dynamic title;
dynamic priority;
dynamic description;
DatumCalendar({
this.status,
this.id,
this.groupId,
this.date,
this.title,
this.priority,
this.description,
});
factory DatumCalendar.fromJson(Map<String, dynamic> json) => DatumCalendar(
status: json["status"],
id: json["_id"],
groupId: json["group_id"],
date: DateTime.parse(json["date"]),
title: json["title"],
priority: json["priority"],
description: json["description"],
);
Map<String, dynamic> toJson() => {
"status": status,
"_id": id,
"group_id": groupId,
"date": date.toUtc(),
"title": title,
"priority": priority,
"description": description,
};
}
由于发布请求似乎适用于地图而不是列表,因此我对我要去的地方感到困惑。我可以就如何正确实施此方法获得一些建议吗?