简体   繁体   English

Flutter firestore QuerySnapshot 在 Android 中没有 getter“文档”的实例

[英]Flutter firestore QuerySnapshot has no instance of getter 'documents' in Android

I am trying to retrieve the values of the variables from the firestore cloud.我正在尝试从 firestore 云中检索变量的值。 Through implementing the code, I've seemed to come across an error, and I've spent hours getting frustrated over this, trying to find solutions here and elsewhere.通过实施代码,我似乎遇到了一个错误,我花了好几个小时为此感到沮丧,试图在这里和其他地方找到解决方案。 The error I've faced alongside the code is as per below.我在代码旁边遇到的错误如下所示。

The following NoSuchMethodError was thrown building StreamBuilder(dirty, state:_StreamBuilderBaseState<QuerySnapshot, AsyncSnapshot>#4939b): Class 'QuerySnapshot' has no instance getter 'documents'.以下 NoSuchMethodError 被抛出构建 StreamBuilder(dirty, state:_StreamBuilderBaseState<QuerySnapshot, AsyncSnapshot>#4939b): Class 'QuerySnapshot' has no instance getter 'documents'。 Receiver: Instance of 'QuerySnapshot' Tried calling: documents接收者:“QuerySnapshot”的实例尝试调用:文件

Error Displayed on the Virtual Device虚拟设备上显示的错误

Code that I am using:我正在使用的代码:

 Widget buildStreamBuilder() {
     return Container(
        child: !_success
            ? Center(
          child: CircularProgressIndicator(
            valueColor: AlwaysStoppedAnimation<Color>(Colors.green),
          ),
        )
            : StreamBuilder(
          stream: FirebaseFirestore.instance.collection('sensors').snapshots(),
          builder: (context, snapshot) {
            if (!snapshot.hasData) {
              return Center(
                child: CircularProgressIndicator(
                  valueColor: AlwaysStoppedAnimation<Color>(Colors.red),
                ),
              );
            } else {
              snapshot.data.documents.forEach((doc) {
                if (doc.documentID == 'cSBKpiEe1XKmQC8BDzMk') {
                  print('current value = ${doc['BatStat']}'); //var1
                  globalCurrentSensorValue = doc['BatStat'].toDouble();
                }
              });
              return Center(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Expanded(
                      flex: 1,
                      child: Container(),
                    ),
                    Expanded(
                      flex: 1,
                      child: Container(),
                    )
                  ],
                ),
              );
            }
          },
        ));
  }

Change this:改变这个:

snapshot.data.documents.forEach((doc)

into this:进入这个:

snapshot.data.docs.forEach((doc)

QuerySnapshot class contains a property called docs which will return all the documents in a collection. QuerySnapshot class 包含一个名为docs的属性,它将返回集合中的所有文档。

if I am not wrong you are accessing the document data incorrectly.如果我没记错的话,您正在错误地访问文档数据。 try this:试试这个:

 if (doc.documentID == 'cSBKpiEe1XKmQC8BDzMk') {
              //use doc.data()[' '] method
              print('current value = ${doc.data()['BatStat']}'); //var1
              globalCurrentSensorValue = doc.data()['BatStat'].toDouble();
            }

暂无
暂无

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

相关问题 Flutter/Firebase - 列表<dynamic>没有实例 getter 'documents'</dynamic> - Flutter/Firebase - List<dynamic> has no instance getter 'documents' 输入“QuerySnapshot?” 没有成员“文件” - Type 'QuerySnapshot?' has no member 'documents' 没有为类型“QuerySnapshot”定义吸气剂“文档”<object?> '</object?> - The getter 'documents' isn't defined for the type 'QuerySnapshot<Object?>' 没有为类型“QuerySnapshot”定义吸气剂“文档” <map<string, dynamic> >' </map<string,> - The getter 'documents' isn't defined for the type 'QuerySnapshot<Map<String, dynamic>>' 为什么 vs 代码说方法 '.getDocuments' 和 getter 文档没有分别为类型 Query 和 QuerySnapshot 定义? - Why is vs code saying the method '.getDocuments' and the getter documents are not defined for types Query and QuerySnapshot respectively? Firestore Flutter:获取 querySnapshot.length 并设置新文档 - Firestore Flutter : get querySnapshot.length & set new doc 如何在 flutter 中缓存 firestore 文档? - How to cache firestore documents in flutter? 我如何从 flutter firestore 中的 querysnapshot 获取单个字段值 - How do i get the single field value from querysnapshot in flutter firestore Flutter/Firestore - 错误:“列表”类型的值<string> Function(QuerySnapshot)' 不能分配给 'List' 类型的变量<dynamic> '</dynamic></string> - Flutter/Firestore - Error: A value of type 'List<String> Function(QuerySnapshot)' can't be assigned to a variable of type 'List<dynamic>' 1 QuerySnapshot 在 Flutter web 应用程序中返回 null 但在 Android 控制台中给出结果 - 1 QuerySnapshot returning null in Flutter web app but gives result in the Android console
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM