firebase - 如何使用DocumentSnapshot而不是QuerySnapshot?

标签 firebase flutter dart google-cloud-firestore

我有QuerySnapshot方法,它从firebase获取一些数据:

  getBookings(AsyncSnapshot<QuerySnapshot> snapshot) {
return snapshot.data.docs
    .map((doc) => CalendarBookingBlock(
          bookingName: doc.data()['bookingName'],
          bookingTime: doc.data()['bookingData'],
        ))
    .toList();
}
但是我必须使用DocumentSnapshot。当我尝试它时,没有为'DocumentSnapshot'类型定义snapshot.data.docs。我应该怎么做才能解决这个问题?

最佳答案

DocumentSnapshot表示单个文档。它不包含其他文件,例如QuerySnapshot。在documentation中有查询单个文档的示例。通常情况如下:

if (snapshot.exists) {
    Map<String, dynamic> data = snapshot.data();
    // document fields are in data
}
else {
    // decide what you want to do if the document doesn't exist
}

关于firebase - 如何使用DocumentSnapshot而不是QuerySnapshot?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64297800/

相关文章:

ios - react-native-firebase notifications().onNotification() 从不在 ios 中调用

javascript - 如果集合为空,如何有效地检查?

firebase - 无法加载providerinstaller模块: No acceptable module found.本地版本为0远程版本为0错误flutter

flutter - 如何使用 FutureBuilder Flutter 叠加并居中圆形进度指示器

Dart 从 base64 转换为 HEX

flutter - 如何在 flutter 中添加圆形评级图表?

ios - Swift 全局变量不更新

javascript - 如何解析 Angular 2中的 "_this is undefined"

android - 在 flutter 中将屏幕分成两个相等的部分

dart - 什么意思??在 Dart