首页 > 解决方案 > 如何从 Firbase 数据库中获取特定数据

问题描述

飞镖

在 Firestore 中,我可以从字段所属的特定文档中获取特定的字段值,就像这样

 final DocumentSnapshot doc = await  FirebaseFirestore.instance.collection("users").doc(widget.documentUid).get();

      String name =  doc.get("name");

这在 Firebase 数据库中也可以实时吗?

我试过了,但没有选择doc.get("name");

FirebaseDatabase.instance.reference().child(currentUser.uid).then((snapshot) {
      String jj = snapshot.value;
    });

好的,可以说我这样设置数据

FirebaseDatabase.instance.reference().child("123").set({
      "test1":"test"
    });

然后我把它输入数据库

 testapp-54-default-rtdb
    123 
    test1": test

我怎样才能得到test field属于123唯一的

非常感谢您阅读我的问题

在此处输入图像描述

标签: firebaseflutterdartfirebase-realtime-databasegoogle-cloud-firestore

解决方案


获取路径的特定数据

FirebaseDatabase.instance.reference().child(currentUser.uid).child("ok")
  .once().then((DataSnapshot snapshot) {
       String jj = snapshot.value;
   });

在您的情况下,这将返回“okkk”。

或者

FirebaseDatabase.instance.reference().child(currentUser.uid)
   .once().then(( DataSnapshot snapshot) {
       String jj = snapshot.value["ok"];
    });

推荐阅读