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:
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
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
_getEventsForDayreturns the correct type according to the expected signature of theeventsLoader.Try to update like this,
3.Ensure that you're providing a list of
Eventsto this function, and it will return a list of events corresponding to the given day. Make sure to handle null events appropriately.In your
eventLoaderproperty, use the modified function like this,4.This ensures that you're passing a list to
_getEventsForDayand handling cases where the events might be null. Also, make sure to handle null values in theeventDateproperty within theEventsclass, as I assumed it might be nullable based on your class definition.