【问题标题】:How could i retrieve data from firebse realtime database in flutter我如何从颤动的firebase实时数据库中检索数据
【发布时间】:2020-12-05 21:52:34
【问题描述】:

我将不同用户的数据保存在他们的 uid 节点中,并且在 uid 节点中我生成了不同的密钥来保存数据。我试图从键节点检索电子邮件、用户名。 我尝试使用以下代码获取电子邮件、用户名:- `

@override
  void initState() {
    super.initState();
    getCurrentUser();
    rootRef.child('Manager').child(loggedInUser.uid).child(accountKey);
    rootRef.once().then((DataSnapshot snap) {
     var value= snap.value;
     print(value['username']);
    }
    );
  }

` 但我得到一个空值。 我如何检索电子邮件、用户名并将其显示到文本小部件。

【问题讨论】:

  • 你试过 rootRef.child('Manager').child(loggedInUser.uid).child(accountKey).once().then((DataSnapshot snap) { var value= snap.value;打印(值['用户名']);});
  • 是的,我试过了,但它不起作用

标签: firebase flutter dart firebase-realtime-database firebase-authentication


【解决方案1】:

您需要使用FutureBuilder(),因为uid 在上述代码中将为空,因此创建一个返回Future<DataSnapshot> 的方法:

   Future<DataSnapshot> getData() async{
     var user = await FirebaseAuth.instance.currentUser();
     final dbRef = FirebaseDatabase.instance.reference().child('Manager').child(user.uid).child(accountKey);
    return await dbRef.once();
  }

然后在FutureBuilder里面使用:

FutureBuilder(
    future: getData(),
    builder: (context, AsyncSnapshot<DataSnapshot> snapshot) {
        if (snapshot.hasData) {

【讨论】:

    猜你喜欢
    • 2018-09-11
    • 1970-01-01
    • 2020-08-20
    • 2019-09-14
    • 2019-04-16
    • 2020-12-09
    • 1970-01-01
    • 2021-05-21
    相关资源
    最近更新 更多