【问题标题】:Flutter - Error: The getter 'docs' isn't defined for the type 'Object'Flutter - 错误:没有为“对象”类型定义吸气剂“文档”
【发布时间】:2021-08-22 17:01:27
【问题描述】:

我正在开发 Flutter 2.2.1(频道稳定版)。我最近将我的 SDK 环境从 2.7.0 更改为 2.12.0 (sdk: ">=2.12.0 <3.0.0") 以添加插件,但我遇到了很多错误(尤其是关于 null 安全性)。其中之一是关于从 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();
          });
    }),

错误是关于docsfinal beacons = snapshot.data!.docs; 行:

getter 'docs' 没有为类型 'Object' 定义。 尝试导入定义“docs”的库,将名称更正为现有 getter 的名称,或定义名为“docs”的 getter 或字段。

我是新的 Flutter 用户,我不明白在这里做什么。感谢您的帮助。

【问题讨论】:

    标签: flutter dart google-cloud-firestore


    【解决方案1】:

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

    StreamBuilder<QuerySnapshot>
    

    【讨论】:

      【解决方案2】:

      需要声明buildersnapshot参数类型为AsyncSnapshot

      例如:

      StreamBuilder(
              stream: FirebaseFirestore.instance.collection('users').snapshots(),
              builder: (BuildContext context, AsyncSnapshot snapshot) {
                if (snapshot.hasData) {
                  List<DocumentSnapshot> docs = snapshot.data!.docs;
                  ...
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-09-29
        • 2021-08-28
        • 2021-07-15
        • 2021-02-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多