[英]How to iterate through fields in a Firestore Document in Flutter?
I have a Firestore Document and I would like to iterate through them.我有一个 Firestore 文档,我想遍历它们。 I'm trying the following code, after looking in this question How to iterate through all fields in a firebase document?在查看此问题后,我正在尝试以下代码如何遍历 firebase 文档中的所有字段?
FutureBuilder(
future: FirebaseFirestore.instance.collection('collection').doc('doc').get();,
builder: (context, AsyncSnapshot<DocumentSnapshot> snapshot){
if (snapshot.hasData) {
Map<String, dynamic> data = snapshot.data! as Map<String, dynamic>;
for (var entry in data.keys) {
// actions
}
}
),
This code results in type '_JsonDocumentSnapshot' is not a subtype of type 'Map<String, dynamic>' in type cast
此代码导致type '_JsonDocumentSnapshot' is not a subtype of type 'Map<String, dynamic>' in type cast
data is a method, not a property on DocumentSnapshot . data是一种方法,而不是DocumentSnapshot上的属性。 Call it using parenthesis:使用括号调用它:
Map<String, dynamic> data = snapshot.data() as Map<String, dynamic>;
Finally I found the solution:最后我找到了解决方案:
FutureBuilder(
future: FirebaseFirestore.instance.collection('collection').doc('doc').get();,
builder: (context, AsyncSnapshot<DocumentSnapshot> snapshot){
if (snapshot.hasData) {
Map<String, dynamic> data = snapshot.data!.data() as Map<String, dynamic>;
for (var match in data.keys) {
// actions
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.