[英]How to get value with key from a Map<String, dyanmic> in Flutter
I am new to flutter and Firestore, and still leaning.我是 flutter 和 Firestore 的新手,还在学习。
I have been trying to print a single value from a Map<String, Dynamic>.我一直在尝试从 Map<String, Dynamic> 打印单个值。 When I print the whole map it works but when tried to print a single value with key, it produces a null error.
当我打印整个 map 时,它可以工作,但是当尝试使用键打印单个值时,它会产生 null 错误。
getUsersFromFireStore(String appUid) async {
List<Map<String, dynamic>> list = [];
await instance.collection('Users').where('refDocument', isEqualTo: appUid).get()
.then((querySnapshotOne) {
return querySnapshotOne.docs[0].reference.collection('Data').orderBy('timeStamp', descending: true).get();
}).then((querySnapshotTwo) {
if(querySnapshotTwo.size > 0){
querySnapshotTwo.docs.forEach((doc) {
String id = doc.id;
list.add(doc.data()); // Here is the problem
// print(doc.data().toString()); produces {age:24, name: Hari Ram}
// But doc.data()['name'] doesn't work, gives null error
});
}
});
return list;
}
Please help.请帮忙。 Thanks in advance.
提前致谢。
** UPDATE ** when tried with doc.data['name']
as per the comments, it shows following error: ** 更新 ** 根据评论尝试使用
doc.data['name']
时,它显示以下错误:
doc.data['name'] // This should solve your problem doc.data['name'] // 这应该可以解决您的问题
Remove () after the data keyword.去掉data关键字后面的()。 ie
print(doc.data['name'])
即
print(doc.data['name'])
UPDATE: Try doc.get('name')
更新:试试
doc.get('name')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.