首页 > 解决方案 > Dart 无法更新地图

问题描述

我的目的是为flutter clean_calendar插件返回一张地图。我的 json 正文返回 List<CalendarActivity> ,我正在尝试创建新地图(Map<DateTime ,List<CleanCalendarEvent>>)。此外,如果我在同一日期有不同的事件,我想将该事件(值)添加到他们的日期(键)。你能帮我看看是什么导致了这个错误吗?如果你有什么建议我想听听。顺便说一句,我是初学者,谢谢

我的课是;

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,
                startTime: element.startingDate,
                endTime: element.endDate,
                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)

标签: listdictionarydart

解决方案


推荐阅读