如何:在 signup/creation 上设置 Firebase 用户的个人资料信息? (颤振网)

How to: Set Firebase user's profile info on signup/creation? (Flutter-web)

如何在创建用户帐户的同时设置用户的显示名称?

我正在使用 createUserWithEmailAndPassword 方法并尝试从同一个表单中的 3 个不同的 TextFormField 获取信息。

下面是我正在尝试做的一个非常简单的例子......希望有人能从中得到帮助..

谢谢

这是我的注册方式:

import 'package:firebase_auth/firebase_auth.dart';

class AuthService {
  FirebaseAuth auth = FirebaseAuth.instance;

  //Create user with email and password (+ displayName)
  signUp({String email, String password, String name}) async {
    await FirebaseAuth.instance
        .createUserWithEmailAndPassword(email: email, password: password);


        // I'd like to create/update the new user's displayName here, using the String value (name) being passed into this function.



  }
}

这是数据来源的示例:

class SignUpForm extends StatelessWidget {
  final GlobalKey<FormState> _formKey = GlobalKey();

  String name;
  String email;
  String password;

  TextEditingController nameController;
  TextEditingController emailController;
  TextEditingController passwordController;

  submit(){
    AuthService().signUp(password: 'password', email: email, name: name);
  }

  @override
  Widget build(BuildContext context) {
    return Form(
      key: _formKey,
      child: Column(
        children: <Widget>[
          TextFormField(
            controller: nameController,
            onChanged: (value) {
              name = value;
            },
          ),
          TextFormField(
            controller: emailController,
            onChanged: (value) {
              email = value;
            },
          ),
          TextFormField(
            controller: passwordController,
            onChanged: (value) {
              password = value;
            },
          ),
        ],
      ),
    );
  }
}
_createUser() async {
await _auth
    .createUserWithEmailAndPassword(
  email: emailText,
  password: passwordText,
)
FirebaseUser user = await _auth.currentUser();

  UserUpdateInfo updateInfo = UserUpdateInfo();
  updateInfo.displayName = 'John Doe';
  await user.updateProfile(updateInfo);
  print('USERNAME IS: ${user.displayName}');
}

用户对象从 createUserWithEmailAndPassword 函数的承诺返回,然后您可以通过向 firebase 发出进一步的请求立即更新 displayName。

 await FirebaseAuth.instance.createUserWithEmailAndPassword(email: email, password: password)
          .then((user){
    var userUpdateInfo = new UserUpdateInfo(); //create user update object
    userUpdateInfo.displayName = "John Doe"
    await firebaseAuth.updateProfile(userUpdateInfo); //update to firebase
    await user.reload(); //reload  user data
})

有关 UserUpdateInfo class 的更多信息,请点击此处: https://pub.dev/documentation/firebase_auth/latest/firebase_auth/UserUpdateInfo-class.html

您可能还想查看 firebase github 存储库中的示例应用程序。我已经链接到与您要实现的目标相关的文件和行:

https://github.com/FirebaseExtended/flutterfire/blob/7ccdd3b9bca948d15b397fe5c86ec4616b611c47/packages/firebase_auth/firebase_auth/example/lib/register_page.dart#L88

编辑

最终工作代码:

class AuthService {
FirebaseAuth auth = FirebaseAuth.instance;

signUp({String email, String password, String name}) async {
await FirebaseAuth.instance
.createUserWithEmailAndPassword(email: email, password: password)
.then(
(value) async {
var userUpdateInfo = new UserUpdateInfo(); //create user update object
userUpdateInfo.displayName = "John Doe";
await value.user.updateProfile(userUpdateInfo); //update to firebase
await value.user.reload();

print('displayname= ${userUpdateInfo.displayName}');
},
);
}
}