Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Passing List<Events> back to eventLoader property from function in flutter app gives error

I am trying to populate events on a table_calendar widget in my Flutter app but I am having trouble working with the events and the eventsLoader: property of the table_calendar widget.

I have figured out that if I use 2 for loops I can populate a list of type Events, List, and pass it back to eventsLoader but I am getting an error I don’t understand.

Here is the code I am using:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

      eventLoader: (day) {
        return _getEventsForDay(day, events);  <<< ERROR HERE
      },

  List<Events>? _getEventsForDay(DateTime day, [List<Events>? events]) {
    final int today = day.day;
    final int nbrEvents = events!.length;

    for (Events allEvent in events) {
      if (day.year == allEvent.eventDate!.year &&
          day.month == allEvent.eventDate!.month &&
          day.day == allEvent.eventDate!.day) {
        // fill this list with all the events associated
        //with the given day and pass the list back
        late final List<Events> listDayEvents = [];
        for (Events dayEvents in events) {
          if (day.year == allEvent.eventDate!.year &&
              day.month == allEvent.eventDate!.month &&
              day.day == allEvent.eventDate!.day) {
            listDayEvents.add(allEvent);
          }
        }
        return listDayEvents;
      }
      List<Events> emptyList = [];
      return emptyList;
    }
  }

The error I am getting on the eventsLoader: property is this:

The return type 'List<Events>?' isn't a 'List<_>', as required by the closure's context.dartreturn_of_invalid_type_from_closure
List<Events>? _getEventsForDay(DateTime day, [List<Events>? events])
Type: List<Events>? Function(DateTime, [List<Events>?])

package:deal_diligence/screens/appointment_calendar.dart

I don’t understand this error message since I am returning a List type.

Here is the Events class. I am getting the events from a Firebase collection and each event has the data element in the Events class so am I passing the wrong type of data?

class Events {
  String? id;
  String? eventName;
  DateTime? eventStartTime;
  String? eventDuration;
  DateTime? eventDate;
  String? eventDescription;
  String? userId;
  String? companyId;

Here is the link to the documentation for table_calendar widget

What am I doing wrong? Please help.
Thanks

>Solution :

1.The issue might be due to the return types and the way you’re handling the function _getEventsForDay.

2.The error specifically points to the closure’s context, stating that the return type isn’t of the correct type _, which is expected by the closure.

Let’s address the issues:

Null Safety: It’s important to handle null safety properly, especially when dealing with Firebase data that might contain nullable values.

Function Return Type: Ensure that the function _getEventsForDay returns the correct type according to the expected signature of the eventsLoader.

Try to update like this,

List<Events> _getEventsForDay(DateTime day, List<Events>? events) {
  List<Events> listDayEvents = [];

  if (events == null) {
    return listDayEvents; // Return an empty list if events is null
  }

  for (Events event in events) {
    if (event.eventDate != null &&
        day.year == event.eventDate!.year &&
        day.month == event.eventDate!.month &&
        day.day == event.eventDate!.day) {
      listDayEvents.add(event);
    }
  }

  return listDayEvents;
}

3.Ensure that you’re providing a list of Events to this function, and it will return a list of events corresponding to the given day. Make sure to handle null events appropriately.

In your eventLoader property, use the modified function like this,

eventLoader: (day) {
  return _getEventsForDay(day, events ?? []); // Pass an empty list if events is null
},

4.This ensures that you’re passing a list to _getEventsForDay and handling cases where the events might be null. Also, make sure to handle null values in the eventDate property within the Events class, as I assumed it might be nullable based on your class definition.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading