[英]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.