icantcode
icantcode

Reputation: 162

Flutter: Unhandled Exception: type 'QuerySnapshot' is not a subtype of type 'Future<dynamic>?'

i am migrating the project to null safety using table_calendar 3.0.0, however the library made a huge change since null safety in terms of loading events, i am still struggling rewriting the whole code with the firebase implementation, any help is appreciated please:

   getEventsList() async {
    bookings = await DatabaseMethods().getEvents(myName);
    fetchEvents(bookings!);
  }
    
      fetchEvents(Future bookings){
        bookings.then((value) {
          value.docs.forEach((element) {
            setState(() {
              listEvent.add((element.data()));
            });
          });
        });
      }
    
      List<dynamic> _getEventsForDay(DateTime day) {
        // Implementation example
        final _kEventSource = Map<DateTime, dynamic>.fromIterable(listEvent,
            key: (item) => item['date'].toDate(),
            value: (item) => item);
        final kEvents = LinkedHashMap(equals: isSameDay, hashCode: getHashCode)
          ..addAll(_kEventSource);
        return kEvents[day] ?? [];
      }

the code of DatabaseMethods() above:

getEvents(String myName) async {
    return await FirebaseFirestore.instance
        .collection("bookings")
        .doc(myName)
        .collection("booking")
        .get();
  }

widget code:

TableCalendar<dynamic>(
              firstDay: kFirstDay,
              lastDay: kLastDay,
              focusedDay: _focusedDay,
              selectedDayPredicate: (day) => isSameDay(_selectedDay, day),
              rangeStartDay: _rangeStart,
              rangeEndDay: _rangeEnd,
              calendarFormat: _calendarFormat,
              rangeSelectionMode: _rangeSelectionMode,
              eventLoader: _getEventsForDay,
              startingDayOfWeek: StartingDayOfWeek.monday,
              calendarStyle: CalendarStyle(
                // Use `CalendarStyle` to customize the UI
                outsideDaysVisible: false,
              ),
              onDaySelected: _onDaySelected,
              onRangeSelected: _onRangeSelected,
              onFormatChanged: (format) {
                if (_calendarFormat != format) {
                  setState(() {
                    _calendarFormat = format;
                  });
                }
              },
              onPageChanged: (focusedDay) {
                _focusedDay = focusedDay;
              },
            ),

error below:

ERROR: E/flutter (14897): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: type 'QuerySnapshot' is not a subtype of type 'Future<dynamic>?'
E/flutter (14897): #0      _CalendarPageState.getEventsList

example referenced: example here

Upvotes: 0

Views: 164

Answers (1)

user10675368
user10675368

Reputation:

the function getEvents returns an all the documents in the referral subcollection. You need to loop through all those documents and save them to an array.

    getEvents(String myName) async {
      List<dynamic> bookings= [];
    
      QuerySnapshot? bookingSnapshot = await FirebaseFirestore.instance
        .collection("bookings")
        .doc(myName)
        .collection("booking")
        .get();
      for(doc in bookingSnapshot.docs){
        final data = doc.data();
        bookings.add(data);
      }
      return bookings;
    }

Upvotes: 2

Related Questions