【发布时间】:2021-01-20 14:10:04
【问题描述】:
我的应用实现了 Firebase 身份验证,每个用户都必须输入他们的显示名称才能注册到应用中。
在我的应用程序中,我希望用户能够更新此显示名称。
逻辑很简单,用户点击“Change Display Name”按钮,一个ModalBottomSheet出现,用户可以输入新的显示名并保存。但是这样一来,当显示名称更改时,屏幕不会更新,直到我热重载或重新启动应用程序。
class Body extends StatelessWidget {
final FirebaseAuth _auth = FirebaseAuth.instance;
@override
Widget build(BuildContext context) {
String displayName = _auth.currentUser.displayName;
final _formKey = GlobalKey<FormState>();
String newDisplayName;
return Scaffold(
appBar: AppBar(),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Current display name is:',
style: TextStyle(fontSize: 20),
),
Text(
'$displayName',
style: TextStyle(fontSize: 25),
),
RaisedButton(
child: Text('Change Display Name'),
onPressed: () {
showModalBottomSheet(
isScrollControlled: true,
context: context,
builder: (context) {
return Padding(
padding: MediaQuery.of(context).viewInsets
child: Form(
key: _formKey,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
TextFormField(
decoration: InputDecoration(
labelText: 'New Display Name',
),
keyboardType: TextInputType.text,
onSaved: (value) {
newDisplayName = value;
},
),
RaisedButton(
child: Text('Change'),
onPressed: () {
_formKey.currentState.save();
_auth.currentUser
.updateProfile(displayName: newDisplayName);
Navigator.pop(context);
},
),
],
),
),
);
},
);
},
),
],
),
),
);
}
}
我尝试在不同的地方调用 .reload,但在页面刷新之前显示名称不会改变。
_auth.currentUser.reload();
编辑:感谢https://stackoverflow.com/users/11921453/twelve的解决方案
使用以下 StreamBuilder 包装 manin 小部件,您将能够立即检索 snapshot.data.displayName。
child: StreamBuilder<User>(
stream: FirebaseAuth.instance.userChanges(),
【问题讨论】:
标签: firebase flutter firebase-authentication flutter-showmodalbottomsheet