gpt4 book ai didi

firebase - 从Firebase获取用户数据

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

我正在尝试从他们的uid获取用户数据,并在文本小部件上使用它。我应该创建getCurrentUser方法来获取数据吗?我曾尝试使用querysnapshots,但它什么也没做。这是代码:

            Expanded(
child: Container(
margin: EdgeInsets.only(top: 10.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text('Name'),
Text('Email@email.com'),
Text('+02112345678'),
],
),
),
),
这是更新:
            Expanded(
child: Container(
margin: EdgeInsets.only(top: 10.0),
child: FutureBuilder(
future: getData(),
builder: (context,
AsyncSnapshot<DocumentSnapshot> snapshot) {
if (snapshot.connectionState ==
ConnectionState.done) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(snapshot.data['Full Name']),
Text(snapshot.data['Email Address']),
Text(snapshot.data['Phone Number']),
],
);
} else if (snapshot.connectionState ==
ConnectionState.none) {
return Text("No Data");
}
return CircularProgressIndicator();
},
),
),
输出是这样的:
enter image description here
enter image description here

最佳答案

您需要使用FutureBuilder小部件,首先创建一种获取用户的方法:

  Future<DocumentSnapshot> getData() async {
FirebaseUser firebaseUser = await FirebaseAuth.instance.currentUser();
return await Firestore.instance
.collection("UserAccount").document(firebaseUser.uid).get();
}
然后在 build方法中:
          FutureBuilder(
future: getData(),
builder: (context, AsyncSnapshot<DocumentSnapshot> snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(snapshot.data["name"]),
Text(snapshot.data["email"]),
Text(snapshot.data["number"]),
],
),
} else if (snapshot.connectionState == ConnectionState.none) {
return Text("No data");
}
return CircularProgressIndicator();
},
),
在上面的代码中,我们使用 FutureBuilder,因为 get()是异步的,那么在 ConnectionState.done内部,您将使用 snapshot.data获取数据。

最好阅读文档,查看所有内容手册:
https://flutter.dev/docs/cookbook

关于firebase - 从Firebase获取用户数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62921547/

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