The Calendar Widget ain't working properly

Viewed 46

Inside Calendar in this property onDateSelected I'm passing date. day in function. The calendar is throwing an error that int isn't the subtype of DateTime, but if I pass the date(which is of DateTime) the calendar isn't working. It's not dynamic, it just reloads whenever I click on it.

onDateSelected: (date) {
                    return _handleData(date);
                  },

OR

onDateSelected: (date) {
                    return _handleData(date.day);
                  },
DateTime? selectedDay;
void _handleData(date) {
      setState(() {
        selectedDay = date;
        // selectedEvent = events[selectedDay] ?? [];
      });
    }
1 Answers

I think your selectedDay datatype is int that's why you getting error that int isn't the subtype of DateTime

DateTime? selectedDay = DateTime.now();

 //Here is Calendar widget
Calendar(
        initialDate: selectedDay,
        events: const {},
        startOnMonday: true,
        isExpandable: true,
        eventDoneColor: Colors.green,
        selectedColor: Colors.pink,
        todayColor: Colors.blue,
        eventColor: Colors.grey,
        onDateSelected: (date) {
          _handleNewDate(date);
        },
      ),

void _handleNewDate(date) {
 setState(() {
   selectedDay = date;
   // selectedEvent = events[selectedDay] ?? [];
   print(selectedDay);
 });
}
Related