Rouslan
Rouslan

Reputation: 11

Table calendar doesn't set dynamically events from firestore

I am using table calendar 3.0.5. When I select single date and set event, it works fine and marker is set. If I want to put the events from Firestore in the calendar, there are no markers and events on the event days. Here is my code.

    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}");

terminal output:

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']

Upvotes: 0

Views: 352

Answers (1)

Rouslan
Rouslan

Reputation: 11

OK, I solved the problem. Sometimes you lose so much time with such simple things. Calendar DateTime is UTC time, which means in my case two hours difference. I simply converted startDate toUtc().

    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');

The output now is with time 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

Upvotes: 1

Related Questions