gpt4 book ai didi

firebase - 我如何在 flutter 中从 Firebase 实时数据库中检索数据

转载 作者:行者123 更新时间:2023-12-03 04:39:49 26 4
gpt4 key购买 nike

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

@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']);
}
);
}
`
但我得到一个空值。
我如何检索电子邮件、用户名并将其显示到文本小部件。

最佳答案

您需要使用 FutureBuilder()自从uid在上面的代码中将为 null,因此创建一个返回 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) {

关于firebase - 我如何在 flutter 中从 Firebase 实时数据库中检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63439362/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com