Laurent Thomas
Laurent Thomas

Reputation: 292

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));
  }

}



Upvotes: 0

Views: 324

Answers (2)

Stephen Foale
Stephen Foale

Reputation: 1

Future<void> getDataFromFireStore() async {
// get appointments
var snapShotsValue = await fireStoreReference
    .collection("ToDoList")
    .where('CalendarType', isNotEqualTo: 'personal')
    .get();

// map meetings

List<Meeting> list = snapShotsValue.docs
    .map((e) => Meeting(
        eventName: e.data()['Subject'],
        from: convertTimeStamp(e.data()['StartTime']), //write your own ()
        to: convertTimeStamp(e.data()['EndTime']),
        background: colorConvert(e.data()['color']),  //write your own ()
        isAllDay: e.data()['isAllDay'],
        recurrenceRule: e.data()['RRULE'],
        recurrenceId: e.id,
        resourceIds: List.from(e.data()['resourceIds']),
        notes: e.data()['notes'],
        address: e.data()['Address'].toString(),
        geolocation: e.data()['Location'],
        calendarType: e.data()['CalendarType'],
        id: e.reference,
        key: e.id))
    .toList();

//get staff then add all to MeetingDataSource

var snapShotsValue2 = await fireStoreReference
    .collection("Users")
    .where('isStaff', isEqualTo: true)
    .get();
List<CalendarResource> resources = snapShotsValue2.docs
    .map((e) => CalendarResource(
          displayName: e.data()['display_name'],
          id: e.reference,
          image: NetworkImage(valueOrDefault<String>(
            e.data()['photo_url'],
            'https',
          )),
        ))
    .toList();

setState(() {
  events = MeetingDataSource(list, resources);
  _employeeCollection = resources;
});

}

Upvotes: 0

Kumar
Kumar

Reputation: 46

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); 

}); 

}

Upvotes: 2

Related Questions