首页 > 解决方案 > Flutter Firebase 我将如何从快照中获取特定数据

问题描述

我目前正在为我的应用程序使用实时数据库。我无法从快照中读取特定用户数据。

我的代码:

  final databaseReference = FirebaseDatabase.instance.reference();

  void getData() {
    databaseReference.once().then((DataSnapshot snapshot) {
      Map<dynamic, dynamic> values = snapshot.value;
      //print(snapshot.value['name']);
      print('Data : ${snapshot.value}');
    });
  }

我的数据库布局:

Users:
     userId:
           name: TEST
           email: TESTEmail@gmail.com
           bio: TESTBio

我希望能够读取具有特定 userId 的个人的姓名(例如 Bdhsaiweuy2731319238121shda),我该怎么做?谢谢你的时间,我真的很感激。

标签: firebaseflutterfirebase-realtime-databasedart

解决方案


如果您知道他们的 UID,则可以直接查找该用户databaseReference.child("Users/Bdhsaiweuy2731319238121shda"),然后从那里读取数据。就像是:

databaseReference.child("Users/Bdhsaiweuy2731319238121shda").once().then((DataSnapshot snapshot) {
  print(snapshot.value['name']);
});

推荐阅读