英文:
firebase user number is showing null
问题
我想要将用户的电话号码显示在屏幕上,但屏幕上显示为null。
String _userId;
@override
Widget build(BuildContext context) {
FirebaseAuth.instance.currentUser().then((user) {
_userId = user.phoneNumber;
});
print(_userId);
return MaterialApp(
home: Scaffold(
appBar: AppBar(),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Container(
child: Text("$_userId") ?? CircularProgressIndicator(),
),
],
),
),
),
);
}
英文:
i want to display the user phone number to the screen but it is showing null on the screen
String _userId;
@override
Widget build(BuildContext context) {
FirebaseAuth.instance.currentUser().then((user) {
_userId = user.phoneNumber;
});
print(_userId);
return MaterialApp(
home: Scaffold(
appBar: AppBar(),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Container(
child: Text("$_userId")
?? CircularProgressIndicator(),
答案1
得分: 2
You need to move your Firebase query to outside your build method. The build method is constantly being run as your Widget is rebuilt. Ideally you would implement it this way:
// _userId needs to be initiated with a blank value
String _userId = '';
@override
void initState() {
loadCurrentUser();
super.initState();
}
void loadCurrentUser(){
FirebaseAuth.instance.currentUser().then((user) {
setState(() {
_userId = user.phoneNumber;
});
});
}
This way, as soon as the information is loaded your view will be updated.
英文:
You need to move your Firebase query to outside your build method. The build method is constantly being run as your Widget is rebuilt. Ideally you would implement it this way:
// _userId needs to be initiated with a blank value
String _userId = '';
@override
void initState() {
loadCurrentUser();
super.initState();
}
void loadCurrentUser(){
FirebaseAuth.instance.currentUser().then((user) {
setState(() {
_userId = user.phoneNumber;
});
});
}
This way, as soon as the information is loaded your view will be updated.
答案2
得分: 2
你需要使用FutureBuilder
,FutureBuilder小部件与Flutter一起提供,使与异步数据源的操作变得简单。因此,您需要执行以下操作:
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Container(
child : FutureBuilder(
future: FirebaseAuth.instance.currentUser(),
builder: (context, snapshot) {
if (snapshot.hasData) {
print(snapshot);
return Text(snapshot.data.uid);
}
// 默认情况下,显示加载中的旋转器。
return CircularProgressIndicator();
}
)
),
],
),
),
),
);
}
future
属性将包含此生成器当前连接的异步计算,可能为空。另外,您应该使用StatefulWidget
。
英文:
You need to use a FutureBuilder
, the FutureBuilder widget comes with Flutter and makes it easy to work with async data sources. Therefore you need to do the following:
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Container(
child : FutureBuilder(
future: FirebaseAuth.instance.currentUser(),
builder: (context, snapshot) {
if (snapshot.hasData) {
print(snapshot);
return Text(snapshot.data.uid);
}
// By default, show a loading spinner.
return CircularProgressIndicator();
}
)
The future
property will contain the asynchronous computation to which this builder is currently connected, possibly null. Also you should be using a Stateful Widget
.
答案3
得分: 1
你应该展示更多的你的代码,但看起来你正在使用一个 StatelessWidget,这意味着在 Future 返回后,build
方法中的 _userId
的值不会自动更新。最简单的方法是使用一个 StatefulWidget,并在你的 Future 中调用 setState
来更新该值。
另外,你的这一行代码 Text("$_userId")?? CircularProgressIndicator()
不会正确工作,因为存在相同的问题,但你需要根据 _userId
的值来显示一个小部件或另一个小部件。
英文:
You should show more of your code but it appears you're using a StatelessWidget which means that after that Future returns, the value of _userId
in the build method is not going to update by itself. The simplest way would be to use a StatefulWidget and call setState
within your Future to update the value.
Also your line Text("$_userId")?? CircularProgressIndicator()
won't work correctly because of the same issue, but you need to show one widget or the other depending on the value of _userId
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论