[英]How to get nested data from firestore data base using flutter?
我將如何能夠從 firestore 中的結構化 map 中獲取特定字段? 特別是 dPic 鏈接。
嘗試了以下
Future _getDataUSerFromDatabase() async {
await FirebaseFirestore.instance.collection("users").doc(FirebaseAuth.instance.currentUser!.uid).get().then((snapshot)async{
if(snapshot.exists){
setState((){
try {
idCode = snapshot.get(FieldPath(const ['details','id-no']).toString());
firstName = snapshot.get(FieldPath(const ['details','first-name']));
lastName = snapshot.get(FieldPath(const ['details','last-name']));
role = snapshot.get(FieldPath(const ['others','role']));
email = snapshot.get(FieldPath(const ['details','email']));
idCode = snapshot.get(FieldPath(const ['details','id-no']).toString());
address = snapshot.get(FieldPath(const ['details','address']));
dPic = snapshot.get(FieldPath(const ['verification','dPic']));
idFront = snapshot.get(FieldPath(const ['verification','IDFront']));
idBack = snapshot.get(FieldPath(const ['verification','IDBack']));
validation = snapshot.get(FieldPath(const ['others','validation']));
//store the data into the list
data = [idCode,firstName,lastName,email,address,role,validation,dPic,idFront,idBack];
} on StateError catch(e) {
print('No nested field exists!');
}
});
}
});
// final List<DocumentSnapshot> documents = result.snapshot;
}
您可以獲得文檔的整個Map<String, dynamic>
,因此您可以像獲取普通Map
一樣簡單地從中獲取值。
Future _getDataUSerFromDatabase() async {
await FirebaseFirestore.instance.collection("users").doc(FirebaseAuth.instance.currentUser!.uid).get().then((snapshot)async{
if(snapshot.exists){
setState((){
Map<String, dynamic> docData = snapshot.data() as Map<String, dynamic>;
try {
/* do the same for others */
dPic = (docData["verification"] as Map<String, dynamic>)["dPic"],
/* do the same for others */
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.