flutter - 错误 : The getter 'docs' isn't defined for the type 'Object'

标签 flutter dart google-cloud-firestore

我正在研究 Flutter 2.2.1( channel 稳定)。我最近将我的 SDK 的环境从 2.7.0 更改为 2.12.0 ( sdk: ">=2.12.0 <3.0.0" ) 以添加插件,但我遇到了很多错误(尤其是关于空安全的错误)。其中之一是关于从 firestore 中提取数据(我正在使用 cloud_firestore: ^2.2.1 )。
我的代码:

StreamBuilder(
    stream: FirebaseFirestore.instance
        .collection('towns/${widget.townId}/beacons')
        .orderBy('monument')
        .snapshots(),
    builder: (ctx, snapshot) {
      if (snapshot.connectionState == ConnectionState.waiting)
        return CircularProgressIndicator();
      final beacons = snapshot.data!.docs; // Error here
      return ListView.builder(
          physics:
              NeverScrollableScrollPhysics(),
          shrinkWrap:
              true,
          itemCount: beacons.length,
          itemBuilder: (ctx, index) {
            if (beacons[index]['visibility'] == true) {
              return BeaconCard(
                title: beacons[index]['title'],
                monument: beacons[index]['monument'],
                image: beacons[index]['image'],
                duration: beacons[index]['duration'],
                distance: 0,
                townId: widget.townId,
                uuid: beacons[index]['uuid'],
                fontsizeValue: widget.fontsizeValue,
                languageId: widget.languageId,
              );
            }
            return Container();
          });
    }),
错误大约是 docs在线final beacons = snapshot.data!.docs; :

The getter 'docs' isn't defined for the type 'Object'. Try importing the library that defines 'docs', correcting the name to the name of an existing getter, or defining a getter or field named 'docs'.


我是一个新的 flutter 用户,我不明白在这里做什么。谢谢你的帮助。

最佳答案

请传递快照的类型。在这种情况下

StreamBuilder<QuerySnapshot>

关于 flutter - 错误 : The getter 'docs' isn't defined for the type 'Object' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67840643/

相关文章:

json - 来自 JSON 的强类型 Dart 对象

flutter - Flutter TextFormField mask

firebase - Flutter/Firebase getter 'length'在null上被调用

Firebase Firestore 将时间戳作为空对象返回

sqlite - 如何从数据库初始化列表(sqflite)

javascript - 如何在 Firebase firestore db 中使用正确的时区保存 future 的到期日期

android - 如何找出哪个 flutter 依赖项使用特定的 google 包(在 F-droid 上被列入黑名单)?

dart - 将参数传递给 initState

flutter - 错误 Flutter Firebase Auth 和 Google 登录

android - 我如何使用 Android Studio 解决 Flutter iOS 错误?