我正在尝试从 firebase 数据库的集合构建一个列表,但我遇到了流构建器快照的问题。
StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance.collection("users").snapshots().where((event) => true),
builder: (context, snapshot) {
if (snapshot.hasData && snapshot.data != null) {
return ListView.builder(
itemBuilder: (listContext, index) =>
buildItem(snapshot.data.docs[index]),
itemCount: snapshot.data.docs.length,
);
}
return Container();
},
));
}
buildItem(doc) {
return (userId != doc['id'])
? GestureDetector(
onTap: () {
//Navigator.push(context, MaterialPageRoute(builder: (context) => Page(docs: doc)));
},
child: Card(
color: Colors.black,
child: Container(
height: 60,
child: Center(
child: Text(doc['name'],
style: TextStyle(
color: Colors.white,
fontSize: 20,
),
),
),
),
),
)
: Container();
}
这是整个错误:
======== Exception caught by widgets library =======================================================
The following StateError was thrown building:
Bad state: field does not exist within the DocumentSnapshotPlatform
When the exception was thrown, this was the stack:
#0 DocumentSnapshotPlatform.get._findKeyValueInMap (package:cloud_firestore_platform_interface/src/platform_interface/platform_interface_document_snapshot.dart:86:7)
#1 DocumentSnapshotPlatform.get._findComponent (package:cloud_firestore_platform_interface/src/platform_interface/platform_interface_document_snapshot.dart:104:41)
#2 DocumentSnapshotPlatform.get (package:cloud_firestore_platform_interface/src/platform_interface/platform_interface_document_snapshot.dart:120:26)
#3 _JsonDocumentSnapshot.get (package:cloud_firestore/src/document_snapshot.dart:92:48)
#4 _JsonDocumentSnapshot.[] (package:cloud_firestore/src/document_snapshot.dart:96:40)
...
====================================================================================================
构建器正在构建列表,但在其正下方给出了一个红色错误部分。
答案 0 :(得分:1)
您的 doc
方法中的 buildItem
是一个 DocumentSnapshot
object,据我所知,它没有定义 []
。
我认为您错过了给 data()
的电话:
userId != doc.data()['id']
如果您在此更改后收到另一条错误消息,请先在 Stack Overflow 上搜索它,因为我知道最近在如何从文档快照中公开类型方面发生了一些变化。