dart 表日历不从firestore动态设置事件

eeq64g8w  于 2023-05-26  发布在  其他
关注(0)|答案(1)|浏览(119)

我用的是台历3.0.5。当我选择单一日期和设置事件,它工作正常,标记设置。如果我想把来自Firestore的事件放在日历中,那么在事件日上没有标记和事件。这是我的代码。

setState(() {
        for (var calendar in_calendarPageProvider.allCalendarsEvents) {
           if (mySelectedEvents[calendar.startDate] != null) {
          mySelectedEvents[calendar.startDate]!.add(calendar);
        } else {
          mySelectedEvents[calendar.startDate] = [calendar];
        }
      }
                           
    });
print('MAP $mySelectedEvents');
print("allCalendarsEvents: ${_calendarPageProvider.allCalendarsEvents}");

终端输出:

flutter: MAP:  {2022-05-26 02:00:00.000: [Instance of 'CalendarModel'], 2022-05-29 02:00:00.000: [Instance of 'CalendarModel']}

flutter: allCalendarsEvents [Instance of 'CalendarModel', Instance of 'CalendarModel']
dxxyhpgq

dxxyhpgq1#

好的,我解决了这个问题。有时候你会在这么简单的事情上浪费很多时间。日历日期时间是UTC时间,这意味着在我的情况下两个小时的差异。我只是将startDate转换为Utc()。

setState(() {
          for (CalendarModel calendar in _calendarPageProvider.allCalendarsEvents) {
            if (mySelectedEvents[calendar.startDate.toUtc()] != null) {
              mySelectedEvents[calendar.startDate.toUtc()]!.add(calendar);

            } else {
              mySelectedEvents[calendar.startDate.toUtc()] = [calendar];
            }
          }
        });
print('MAP $mySelectedEvents');

现在的输出是时间00:00:00.000:

flutter: MAP:  {2022-05-26 00:00:00.000: [Instance of 'CalendarModel'], 2022-05-29 00:00:00.000: [Instance of 'CalendarModel']}

and all events are displayed

相关问题