Dart 无法更新地图

问题描述

我的目的是为 Flutter clean_calendar 插件返回一张地图。我的 json 主体返回 List 并且我尝试创建新地图(Map>) 。另外,如果我在同一日期有不同的事件,我想将该事件(值)添加到它们的日期(键)中。你能帮我看看是什么导致了这个错误,如果你有任何建议,我想听听。 顺便说一句,我是初学者, 谢谢

我的课程是;

class Calendaractivity {
  final String title;
  final DateTime startingDate;
  final DateTime endDate;

  Calendaractivity({
    required this.title,required this.startingDate,required this.endDate,});

  factory Calendaractivity.fromJson(Map<String,dynamic> json) {
    return Calendaractivity(
      title: json['baslik'] as String,startingDate: DateTime.parse(json["baslatarih"]),endDate: DateTime.parse(json["bitistarih"]),);
  }
}



class CleanCalendarEvent {
  String summary;
  String description;
  String location;
  DateTime startTime;
  DateTime endTime;
  Color color;
  bool isAllDay;
  bool isDone;

  CleanCalendarEvent(this.summary,{this.description = '',this.location = '',required this.startTime,required this.endTime,this.color = Colors.blue,this.isAllDay = false,this.isDone = false});
}

我的方法是;

Map<DateTime,List<CleanCalendarEvent>>
    listofCalendaractivityToMapOfCleanCalendarEvent(
        List<Calendaractivity> listofCalendaractivity) {

  final map = <DateTime,List<CleanCalendarEvent>>{};
  

  for (var i = 0; i < listofCalendaractivity.length; i++) {
    map.update(
        DateTime(2020,10,2),(value) => value
          ..add(CleanCalendarEvent(listofCalendaractivity[i].title,startTime: listofCalendaractivity[i].startingDate,endTime: listofCalendaractivity[i].endDate)));
  }
  
  return map;
}

但是如果我这样做而不是 for 循环映射值总是从 ifabsent() 函数获取它。

 listofCalendaractivity.forEach(
        (element) {
      map.update(
          element.startingDate,(list) =>
          list
            ..add(
              CleanCalendarEvent(
                'ali',description: 'ali',startTime: element.startingDate,endTime: element.endDate,),ifAbsent: () =>
          [
            CleanCalendarEvent(element.title,description: element.title,color: Colors.grey),]);
    },);

我的错误

======== Exception caught by widgets library =======================================================
The following ArgumentError was thrown building FutureBuilder<List<Calendaractivity>>(dirty,state: _FutureBuilderState<List<Calendaractivity>>#8bfea):
Invalid argument (key): Key not in map.: Instance of 'DateTime'

The relevant error-causing widget was: 
  FutureBuilder<List<Calendaractivity>> file:///D:/Flutter/Github/cbkmobil/lib/screens/calenderPage.dart:30:16
When the exception was thrown,this was the stack: 
#0      MapMixin.update (dart:collection/maps.dart:154:5)
#1      listofCalendaractivityToMapOfCleanCalendarEvent (package:cbkmobil/screens/calenderPage.dart:79:9)
#2      _CalenderPageState.build.<anonymous closure> (package:cbkmobil/screens/calenderPage.dart:40:25)
#3      _FutureBuilderState.build (package:Flutter/src/widgets/async.dart:775:55)
#4      StatefulElement.build (package:Flutter/src/widgets/framework.dart:4691:27)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...