【问题标题】:Flutter + Firebase updateDisplayName when creating account with Email and Password使用电子邮件和密码创建帐户时 Flutter + Firebase updateDisplayName
【发布时间】:2019-07-10 02:30:51
【问题描述】:

我是 Flutter 的新手,对 firebase 非常非常陌生。

我正在尝试通过 createUserWithEmailAndPasswordMethod 创建用户。 我已成功创建,但我试图通过允许用户输入所需的用户名并将所述用户名设置为 displayName 属性来改进它。

我的代码如下:

    _createUser() async {
UserUpdateInfo updateInfo = UserUpdateInfo();
updateInfo.displayName = _usernameController.text;

FirebaseUser user = await _auth
    .createUserWithEmailAndPassword(
  email: _emailController.text,
  password: _passwordController.text,
)
    .then((user) {
  user.updateProfile(updateInfo);
});
print('USERNAME IS: ${user.displayName}');
}

问题是当我运行应用程序时,它总是抛出这个异常:

NoSuchMethodError: The getter 'displayName' was called on null.

每次我调试user 变量时,都会显示为空,即使用户已创建并且我可以打印电子邮件和密码!

我猜问题是Firebase user 为空,但即使我将print('USERNAME IS: ${user.displayName}'); 移到updateProfile 之后,也会发生同样的情况。

希望大家帮忙! 谢谢。

【问题讨论】:

    标签: firebase login dart flutter firebase-authentication


    【解决方案1】:

    您不应该将 await 和 then 一起使用。 await 是 then 方法的替代品。

    _createUser() async {
    await _auth
        .createUserWithEmailAndPassword(
      email: _emailController.text,
      password: _passwordController.text,
    )
    FirebaseUser user = await _auth.currentUser();
    
      UserUpdateInfo updateInfo = UserUpdateInfo();
      updateInfo.displayName = _usernameController.text;
      user.updateProfile(updateInfo);
      print('USERNAME IS: ${user.displayName}');
    }
    

    【讨论】:

    • 不,同样的事情也会发生。
    • @DiogoMelo 抱歉,我错过了一些大事。我现在修复了我的代码。使用await时,不需要使用then()
    • 我找到了一种让它工作的方法,但它实际上只有在我添加 await 后才有效。
    【解决方案2】:

    所以,对我有用的是:我必须在 updateProfile() 之后调用 reload() 方法来获取新的用户信息。经过一些更改后,方法如下所示:

      _createUser() async {
    UserUpdateInfo updateInfo = UserUpdateInfo();
    updateInfo.displayName = _usernameController.text;
    
    await _auth
        .createUserWithEmailAndPassword(
      email: _emailController.text,
      password: _passwordController.text,
    )
        .then((user) async {
      await user.updateProfile(updateInfo);
      await user.reload();
      FirebaseUser updatedUser = await _auth.currentUser();
      print('USERNAME IS: ${updatedUser.displayName}');
      Navigator.of(context).push(
        MaterialPageRoute<Map>(
          builder: (BuildContext context) {
            return Posts(_googleSignIn, updatedUser);
          },
        ),
      );
    }).catchError((error) {
      print('Something Went Wrong: ${error.toString()}');
    });
    }
    

    【讨论】:

    • 你不应该同时使用 await 和 then 。看看我的代码。它会起作用的。
    • 这对我有用,但 user.reload() 是不必要的。
    【解决方案3】:

    如果有人还在寻找,这就是 2021 年的可行解决方案。

    UserCredential userCred = await _auth.createUserWithEmailAndPassword(email, password);
    
    await userCred.user.updateProfile(displayName: "Your Name");
    

    【讨论】:

      猜你喜欢
      • 2020-10-08
      • 2021-07-25
      • 2019-06-18
      • 2019-07-26
      • 2019-05-22
      • 2018-03-12
      • 2016-11-27
      • 1970-01-01
      • 2017-11-25
      相关资源
      最近更新 更多