Firebase“ CurrentUser displayName”将不会随着底部表单更新

问题描述

我的应用程序已实现Firebase身份验证,每个用户都必须输入其 显示名称 才能注册到应用程序中。

在我的应用程序内部,我希望用户能够更新此 显示名称

逻辑很简单,用户单击“ 更改显示名称”按钮,将出现一个 ModalBottomSheet ,用户可以在其中输入其新显示名称并保存。但是通过这种方式,更改显示名称后屏幕不会得到更新,直到我重新加载或重新启动应用程序为止。

enter image description here

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(),

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)