Search code examples
fluttercalendartype-mismatch

type 'String' is not a subtype of type 'Event'


I am adding the table_calendar to my flutter app. This has been a very slow process but I have learned a lot about it. I have another error when creating the markers for the events.

This is the error: type 'String' is not a subtype of type 'Event' I have done some reading on this issue and I know that there is a type mismatch. I'm not sure how to fox this or why it is happening.

The Event class is structured this way:

final String? eventName;
  final DateTime? eventStartTime;
  final String? eventDuration;
  final DateTime? eventDate;
  final String? eventDescription;
  final String? agentId;
  final String? agencyId;

This is where I load the event data into a List variable.

List<Event> _getEventsForDay(DateTime day, [List<Event>? events]) {
    // kEvents is a linkedHashMap
    for (int i = 0; i < eventDoc.length; i++ ) {
      DateTime eventDate = eventDoc[i].eventDate;
      DateTime eventDateUTC = eventDate.toUtc();
      if (day.year == eventDate.year && day.day == eventDate.day && day.month == eventDate.month) {
        List<Event> eventList = [];
        //eventList.add(eventDoc[i].agencyId);
        //eventList.add(eventDoc[i].agentId);
        //eventList.add(eventDoc[i].eventDate);
        //eventList.add(eventDoc[i].eventDescription);
        //eventList.add(eventDoc[i].eventDuration);
        eventList.add(eventDoc[i].eventName);
        //eventList.add(eventDoc[i].eventStartTime);
        //print('kEvents: $kEvents');
        return (kEvents.putIfAbsent(eventDateUTC, () => eventList))??[];

      }
    }
    return [];
  }

Which ever element in the list eventList.add(eventDoc[i] is first to get data is the element that throws the error. So, I know that these are not matching with the API somewhere.

Here is the code (I think this is the correct code) from the API along with the link to the API: https://github.com/aleksanderwozniak/table_calendar/blob/master/lib/src/customization/calendar_builders.dart

// Signature for a function that creates a single event marker for a given `day`.
/// Contains a single `event` associated with that `day`.
typedef SingleMarkerBuilder<T> = Widget? Function(
    BuildContext context, DateTime day, T event);

/// Signature for a function that creates an event marker for a given `day`.
/// Contains a list of `events` associated with that `day`.
typedef MarkerBuilder<T> = Widget? Function(
    BuildContext context, DateTime day, List<T> events);

This is where _getEventsForDay is called:

Widget _buildTableCalendar(List<Event>? events) {
    return TableCalendar(
      firstDay: kFirstDay,
      lastDay: kLastDay,
      focusedDay: _focusedDay,
      selectedDayPredicate: (day) => isSameDay(_selectedDay, day),
      locale: 'en_US',
      eventLoader: (day) {
        return _getEventsForDay(day, events);
      },

This is where I get and pass the Events data from the Firestore:

StreamBuilder(
            //stream: _firestoreService.getEventStream(_selectedDay),
            stream: _db.collection('agency').doc(globals.agencyId).collection('event')
              .where('eventDate', isGreaterThanOrEqualTo: kFirstDay)
              .where('eventDate', isLessThanOrEqualTo: kLastDay)
                    .snapshots().map((snapshot) => snapshot.docs
              .map((document) => Event.fromFirestore(document.data()))
              .toList()),
            builder: (context, AsyncSnapshot <List<Event>> eventsSnapShot) {

              if (eventsSnapShot.hasData) {
                eventDoc = eventsSnapShot.data;
                //print('eventDoc: ' + eventDoc.toString());
                return _buildTableCalendar(eventsSnapShot.data);
              } else {
                return CircularProgressIndicator();
              }

Where is the type mismatch? How do I fix this?


Solution

  • EventDoc[i] is an event and can be added to the list, but EventDoc[i].agencyId is a string and cannot be added to the list, just like the other attributes. To add the event to the list just do:

    eventList.add(eventDoc[i]);