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