firebase - 使用电子邮件和密码创建帐户时,Flutter + Firebase updateDisplayName

标签 firebase authentication dart flutter firebase-authentication

我是 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 变量也一直显示为 null,即使用户已创建并且我可以打印电子邮件和密码!

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

希望大家帮帮忙! 谢谢。

最佳答案

您不应该同时使用 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}');
}

关于firebase - 使用电子邮件和密码创建帐户时,Flutter + Firebase updateDisplayName,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54717930/

相关文章:

android - url_launcher : canLaunch/launch don't work on iOS for simple URL scheme?

flutter - 静态扩展函数在 Dart 中不起作用

javascript - 只在 Firebase 中获取特定的 child

flutter - 尝试分配新值时,无法将元素类型 'int' 分配给映射值类型 'FieldValue'

authentication - 无法通过 jboss-web.xml 设置安全域

Oracle Apex 登录页面

flutter - 完成后重播视频,并显示用户单击了重播图标

android gradle 错误 : libraries must use the exact same version specification. 找到版本 28.0.0

java - 从 Firebase 读取数据时出现 NullPointerException

android - 是否可以使用移动的指纹扫描仪作为生物识别设备在服务器上进行身份验证?