如何将displayName添加到Firebase用户? (扑/飞镖) [英] How can I add the displayName to the Firebase user? (Flutter/Dart)

查看:57
本文介绍了如何将displayName添加到Firebase用户? (扑/飞镖)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用Firebase身份验证保存电子邮件和密码。我还使用Cloud Firestore保存了此信息。但是注册后如何添加和保存 displayName

I can save the email and the password with Firebase Authentication. I also save this information with Cloud Firestore. But how can I add and save the displayName after registering?

我的代码:

Future registerWithEmailAndPassword(String email, String password) async {
try {
  AuthResult result = await _auth.createUserWithEmailAndPassword(
      email: email, password: password);
  FirebaseUser user = result.user;

  // create a new document for the user with the uid
  await DatabaseService(uid: user.uid)
      .updateUserData('User', user.email, 'test.de/test.png', user.uid);
  return _userFromFirebaseUser(user);
} catch (e) {
  print(e.toString());
  return null;
}

}

注册表格按钮:

onPressed: () async {
  if (_formKey.currentState.validate()) {
    setState(() => loading = true);
    dynamic result = await _auth
        .registerWithEmailAndPassword(
            email, password);
    if (result == null) {
      setState(() {
        error = 'Valid email, please';
        loading = false;
      });
     }
    }
   }


推荐答案

您可以使用 updateProfile 方法来更新名称和照片url。

You can use updateProfile method given in the FirebaseUser class to update the name and photo url.

updateProfile({String displayName, String photoURL}).

对于电子邮件,您可以使用其他方法
updateEmail(String newEmail)是异步方法。

For email you can use the different method updateEmail(String newEmail) which is a async method.

或者直接将用户名保存到Firestore中,成功登录后,可以使用FirebaseFirestore的 set方法保存电子邮件,密码和用户名。

Or to save the username directly into the firestore, after successful login you can use 'set' method of the FirebaseFirestore to save email, password and the username.

这篇关于如何将displayName添加到Firebase用户? (扑/飞镖)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆