CalendarDataSource not responding to StreamProvider fetching from Firestore

class HomeView extends StatelessWidget {
  HomeView();

  @override
  Widget build(BuildContext context) {
    
    //Please don't bother about this. Nothing to do with the question
    final homeViewModel = Provider.of<HomeViewModel>(context, listen: false);
     
    //Stream provider to get events from Firestore. Works as intended BUT Calendar does not update the dates.     
    final eventList = Provider.of<List<Event>>(context, listen: true);
    
    //Prints as intended BUT Calendar does not update the dates.
    print(eventList);
   
    return SfCalendar(

      view: CalendarView.month,
      headerStyle: CalendarHeaderStyle(
        textAlign: TextAlign.center,
      ),
      monthViewSettings: MonthViewSettings(showAgenda: true),
      initialSelectedDate: DateTime.now(),
      showCurrentTimeIndicator: true,
      dataSource: EventDataSource(eventList),
      controller: homeViewModel.controller,
      onLongPress: homeViewModel.longPressCalenderElement,
    );
  }
}

//Event related data. Please do not mind the json_related information.
class Event {
  final String recurrenceRule;
  final bool isAllDay;
  final String description;

  @JsonKey(toJson: _dateTimeToJson, fromJson: _dateTimeFromJson)
  final DateTime startTime;
  @JsonKey(toJson: _dateTimeToJson, fromJson: _dateTimeFromJson)
  final DateTime endTime;

  final String subject;

  @JsonKey(toJson: _colorToJson, fromJson: _colorFromJson)
  final Color color;

  @JsonKey(includeIfNull: false)
  final String? documentIDFireStore;

  Event({
    this.documentIDFireStore,
    this.recurrenceRule = '',
    this.isAllDay = false,
    this.description = '',
    required this.startTime,
    required this.endTime,
    this.subject = 'No subject',
    this.color = Colors.lightBlue,
  });

//Skip the Json related information. Please do not mind them.

  Map<String, dynamic> toJson() => _$EventToJson(this);
  factory Event.fromJson(Map<String, dynamic> json,String documentID) => _$EventFromJson(json, documentID);

  //Helper functions for @JsonKey
  static String _dateTimeToJson(DateTime dateTime) =>
      dateTime.toIso8601String();

  static DateTime _dateTimeFromJson(String dateTime) =>
      DateTime.parse(dateTime);

  static String _colorToJson(Color color) => color.value.toRadixString(16);

  static Color _colorFromJson(String colorString) =>
      Color(int.parse(colorString, radix: 16));
 
}

class EventDataSource extends CalendarDataSource {
  EventDataSource(List<Event> source) {
    this.appointments = source;
  }

  @override
  DateTime getStartTime(int index) => (appointments![index] as Event).startTime;

  @override
  DateTime getEndTime(int index) => (appointments![index] as Event).endTime;

  @override
  Color getColor(int index) => (appointments![index] as Event).color;

  @override
  String getRecurrenceRule(int index) =>
      (appointments![index] as Event).recurrenceRule;

  @override
  String getSubject(int index) => (appointments![index] as Event).subject;

  @override
  String getNotes(int index) => (appointments![index] as Event).description;

  @override
  bool isAllDay(int index) => (appointments![index] as Event).isAllDay;
}


2 Replies

DA Dave June 16, 2021 09:41 AM UTC

Hi there,

In the above code snippet, I overrode the == property but did not include it in the code snippet as I thought it adds too much noise.

 However, that seems to be the culprit and issue has been solved by removing it.


IR Indumathi Ravichandran Syncfusion Team June 16, 2021 02:48 PM UTC

Hi Dave, 
 
Thank you for contacting Syncfusion support. 
 
Based on the last update, we are glad to know that the issue resolved at your end. Please get in touch with us if you would require any further assistance. 
 
Regards, 
Indumathi R 


Loader.
Up arrow icon