繁体   English   中英

如何使用 flutter 从 firestore 数据库获取嵌套数据?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM