【问题标题】:Firebase "CurrentUser displayName" won't update with bottomsheetFirebase“CurrentUser displayName”不会随底页更新
【发布时间】: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


    【解决方案1】:

    Firebase 有一个用于监听用户更改的流。在您的 _auth 旁边定义此流。

    使用 au streambuilder 包装您的小部件树并将值设置为定义的流。

    现在每次用户数据更改时都会重新构建小部件树。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-11
      • 1970-01-01
      • 2020-08-08
      • 2018-03-07
      • 2020-08-10
      • 1970-01-01
      相关资源
      最近更新 更多