从Firebase获取用户数据 [英] Get user data from Firebase
问题描述
我正在尝试从用户uid中获取用户数据,并将其用于文本窗口小部件。我应该创建getCurrentUser方法来获取数据吗?我曾尝试使用querysnapshots,但它什么也没做。以下是代码:
Expanded(
子级:Container(
边距:EdgeInsets.only(top:10.0),
子项:Column(
crossAxisAlignment:CrossAxisAlignment.start,
子项:< Widget> [
Text('Name'),
Text('Email @ email .com'),
Text('+ 02112345678'),
],
),
),
),
这里是更新内容:
Expanded(
子级:容器(
保证金:EdgeInsets.only(top:10.0),
子代:FutureBuilder(
未来:getData(),
构建器:(上下文,
AsyncSnapshot< DocumentSnapshot> ;快照){
if(snapshot.connectionState ==
ConnectionState.done){
return Column(
crossAxisAlignment:CrossAxisAlignment.start,
子级:< Widget> [
Text(snapshot.data ['Full Name']),
Text(snapshot.data ['Email Address']),
Text(snapshot.data ['Phone Number']),
],
);
}否则,如果(snapshot.connectionState ==
ConnectionState.none){
return Text( No Data);
}
返回CircularProgressIndicator();
},
),
),
输出是这样的: / p>
您需要使用 FutureBuilder
小部件,首先创建一个方法来获取用户:
Future< DocumentSnapshot> getData()async {
FirebaseUser firebaseUser =等待FirebaseAuth.instance.currentUser();
返回等待Firestore.instance
.collection( UserAccount)。document(firebaseUser.uid).get();
}
然后在 build
方法中:
FutureBuilder(
future:getData(),
builder:(context,AsyncSnapshot< DocumentSnapshot>快照){
if(snapshot.connectionState == ConnectionState.done){
return Column(
crossAxisAlignment:CrossAxisAlignment.start,
子级:< Widget> [[
Text(snapshot.data [名称]),
文本(snapshot.data [电子邮件]),
文本(snapshot.data [数字]),
],
),
}否则,如果(snapshot.connectionState == ConnectionState.none){
return Text( No data);
}
return CircularProgressIndicator();
},
),
在上面的代码中,我们使用 FutureBuilder
,因为 get()
是异步的,然后在 ConnectionState.done
,您将使用 snapshot.data
获取数据。
最好阅读文档,查看所有内容手册:
https://flutter.dev/docs/cookbook
I'm trying to get user data from their uid and use it on text widget. Should I create getCurrentUser method to get the data? I have tried using querysnapshots but it did nothing. Here is the code :
Expanded(
child: Container(
margin: EdgeInsets.only(top: 10.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text('Name'),
Text('Email@email.com'),
Text('+02112345678'),
],
),
),
),
Here is the update :
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();
},
),
),
The output was like this :
You need to use the FutureBuilder
widget, first create a method to get the user:
Future<DocumentSnapshot> getData() async {
FirebaseUser firebaseUser = await FirebaseAuth.instance.currentUser();
return await Firestore.instance
.collection("UserAccount").document(firebaseUser.uid).get();
}
Then inside the build
method:
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();
},
),
In the above code, we use a FutureBuilder
since get()
is asynchronous, then inside ConnectionState.done
you will get the data using snapshot.data
.
It's better if you read the documentation, check all the flutter cookbook:
https://flutter.dev/docs/cookbook
这篇关于从Firebase获取用户数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!