[英]Flutter/Firebase - List<dynamic> has no instance getter 'documents'
[英]Flutter firestore QuerySnapshot has no instance of getter 'documents' in Android
我正在嘗試從 firestore 雲中檢索變量的值。 通過實施代碼,我似乎遇到了一個錯誤,我花了好幾個小時為此感到沮喪,試圖在這里和其他地方找到解決方案。 我在代碼旁邊遇到的錯誤如下所示。
以下 NoSuchMethodError 被拋出構建 StreamBuilder(dirty, state:_StreamBuilderBaseState<QuerySnapshot, AsyncSnapshot>#4939b): Class 'QuerySnapshot' has no instance getter 'documents'。 接收者:“QuerySnapshot”的實例嘗試調用:文件
我正在使用的代碼:
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(),
)
],
),
);
}
},
));
}
改變這個:
snapshot.data.documents.forEach((doc)
進入這個:
snapshot.data.docs.forEach((doc)
QuerySnapshot
class 包含一個名為docs
的屬性,它將返回集合中的所有文檔。
如果我沒記錯的話,您正在錯誤地訪問文檔數據。 試試這個:
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.