Search code examples
flutterfirebasedartsyncfusion

Flutter - populating syncfusion calendar with data from Firebase


I am using the syncfusion_flutter_calendar package. My objective is to populate the calendar with data coming from Firestore. When I try the code below, I am getting an error that I understand, but I do not find where to fix it. Please, can you help? Thank you.

Error : Unhandled Exception: type 'List' is not a subtype of type 'List'


var myQueryResult;

List<Color> _colorCollection = <Color>[];
MeetingDataSource? events;

final databaseReference = FirebaseFirestore.instance;

class CalendarLastTest extends StatefulWidget {
  const CalendarLastTest({Key? key}) : super(key: key);

  @override
  State<CalendarLastTest> createState() => _CalendarLastTestState();
}

class _CalendarLastTestState extends State<CalendarLastTest> {

  @override
  void initState() {
    _initializeEventColor();
    getDataFromFireStore().then((results) {
      SchedulerBinding.instance.addPostFrameCallback((timeStamp) {
        setState(() {});
      });
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('TEST AGENDA'),
      ),

      body: SfCalendar(
      view: CalendarView.month,

      initialDisplayDate: DateTime.now(),
      dataSource: events,
      monthViewSettings: const MonthViewSettings(
            appointmentDisplayMode: MonthAppointmentDisplayMode.indicator,
            showAgenda: true),
    ),
    );
  }


  Future<void> getDataFromFireStore() async {
    var snapShotsValue = await myQuery();
    final Random random =  Random();
    List<Meeting> list = snapShotsValue.docs
        .map((e) => Meeting(
        title: e.data()['name'],
        description: e.data()['notes'],
        from: DateFormat('yyyy-MM-dd HH:mm').parse(e.data()['start_Date']),
        to: DateFormat('yyyy-MM-dd HH:mm').parse(e.data()['due_Date']),
        backgroundColor: _colorCollection[random.nextInt(9)],
        isAllDay: false))
        .toList();

    setState(() {
      events = MeetingDataSource(list);
      print (events);
    });
 }

  Future myQuery () async {

  //  final provider = Provider.of<MeetingProvider>(context, listen: false);
    //final provider = Provider.of<MeetingProvider> (context);
    final uid = FirebaseAuth.instance.currentUser!.uid;
    final path = 'Users/$uid/allTasks';
    final currentQuery = FirebaseFirestore.instance.collection(path);

    myQueryResult = currentQuery.where('done', isEqualTo : 'No');

    myQueryResult =
        myQueryResult.where('start_Date', isNotEqualTo: '');

    //  myQueryResult = myQueryResult.where('due_Date'.length, isEqualTo : 16);

    final snapshot = await myQueryResult.get();

          return snapshot;
      }
  void _initializeEventColor() {
    _colorCollection = <Color>[];
    _colorCollection.add(const Color(0xFF0F8644));
    _colorCollection.add(const Color(0xFF8B1FA9));
    _colorCollection.add(const Color(0xFFD20100));
    _colorCollection.add(const Color(0xFFFC571D));
    _colorCollection.add(const Color(0xFF36B37B));
    _colorCollection.add(const Color(0xFF01A1EF));
    _colorCollection.add(const Color(0xFF3D4FB5));
    _colorCollection.add(const Color(0xFFE47C73));
    _colorCollection.add(const Color(0xFF636363));
    _colorCollection.add(const Color(0xFF0A8043));
  }

}




Solution

  • The issue is that the children's type is ListMeeting> the map method did not return that information, resulting in the type exception. You must specify the type of argument (Meeting) to the map method in order to fix this error. Please see the code snippets below.

    Future<void> getDataFromFireStore() async 
    

    {

    var snapShotsValue = await myQuery(); 
    
    final Random random = Random(); 
    
    List<Meeting> list = snapShotsValue.docs 
    
        .map<Meeting>((e) => Meeting( 
    
        eventName: e.data()['name'], 
    
        // description: e.data()['notes'], 
    
        from: DateFormat('yyyy-MM-dd HH:mm').parse(e.data()['start_Date']), 
    
        to: DateFormat('yyyy-MM-dd HH:mm').parse(e.data()['due_Date']), 
    
        background: _colorCollection[random.nextInt(9)], 
    
        isAllDay: false)) 
    
        .toList(); 
    
    setState(() { 
    
        events = MeetingDataSource(list); 
    
    }); 
    

    }