简体   繁体   English

Flutter:未处理的异常:类型“QuerySnapshot”不是“Future”类型的子类型<dynamic> ?

[英]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:我正在使用 table_calendar 3.0.0 将项目迁移到 null 安全性,但是由于加载事件方面的 null 安全性,该库做出了巨大的改变,我仍在努力用 firebase 实现重写整个代码,请提供任何帮助:

   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:上面 DatabaseMethods() 的代码:

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参考示例:此处示例

the function getEvents returns an all the documents in the referral subcollection.函数getEvents返回引用子集合中的所有文档。 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;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未处理的异常:输入“未来”<QuerySnapshot> &#39; 不是 &#39;QuerySnapshot&#39; 类型的子类型 - Unhandled Exception: type 'Future<QuerySnapshot>' is not a subtype of type 'QuerySnapshot' 未处理的异常:输入“未来”<dynamic> ' 不是类型 'String' 的子类型:Flutter 异常</dynamic> - Unhandled Exception: type 'Future<dynamic>' is not a subtype of type 'String' : Flutter Exception 未处理的异常:输入“未来”<dynamic> ' 不是 dart/flutter 中“ProductModel”类型的子类型</dynamic> - Unhandled Exception: type 'Future<dynamic>' is not a subtype of type 'ProductModel' in dart/flutter Flutter:未处理的异常:输入“未来” <list<dynamic> &gt;' 不是类型“列表”的子类型<dynamic> ' 在类型转换中</dynamic></list<dynamic> - Flutter: Unhandled Exception: type 'Future<List<dynamic>>' is not a subtype of type 'List<dynamic>' in type cast Flutter 错误:未处理的异常:类型“未来”<dynamic> ' 不是类型 'Future 的子类型<null> ?</null></dynamic> - Flutter Error : Unhandled Exception: type 'Future<dynamic>' is not a subtype of type 'Future<Null>? 未处理的异常:输入“未来”<dynamic> &#39; 不是类型 &#39;Future<alertDialogAction> &#39; - Unhandled Exception: type 'Future<dynamic>' is not a subtype of type 'Future<alertDialogAction>' '未来<querysnapshot> ' 不是类型 'Stream 的子类型<dynamic> '?</dynamic></querysnapshot> - 'Future<QuerySnapshot>' is not a subtype of type 'Stream<dynamic>'? 未处理的异常:键入&#39;_InternalLinkedHashMap <String, dynamic> &#39;不是&#39;Future类型的子类型 <dynamic> &#39; - Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Future<dynamic>' 未处理的异常:输入“未来”<dynamic> ' 不是类型 'String' 的子类型</dynamic> - Unhandled Exception: type 'Future<dynamic>' is not a subtype of type 'String' 未处理的异常 Future 动态不是 FutureOr List Books 类型的子类型 - Unhandled Exception Future dynamic is not a subtype of type FutureOr List Books
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM