Chr*_*ris 1 dart firebase flutter flutter-web
如何在创建用户帐户的同时设置用户的显示名称?
我正在使用 createUserWithEmailAndPassword 方法并尝试从同一表单中的 3 个不同 TextFormFields 获取信息。
下面是我正在尝试做的一个非常简单的例子......希望有人能够帮助......
谢谢
这是我的注册方法:
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.
}
}
Run Code Online (Sandbox Code Playgroud)
这是数据来自何处的示例:
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;
},
),
],
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
用户对象从 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
})
Run Code Online (Sandbox Code Playgroud)
有关 UserUpdateInfo 类的更多信息,请访问:https ://pub.dev/documentation/firebase_auth/latest/firebase_auth/UserUpdateInfo-class.html
您可能还想查看 firebase github 存储库上的示例应用程序。我已链接到与您要实现的目标相关的文件和行:
编辑
最终工作代码:
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}');
},
);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2580 次 |
| 最近记录: |