Flutter:使用从Firebase身份验证创建的帐户在Firestore中创建用户 [英] Flutter: Creating a user in Firestore using the account created from Firebase Authentication
问题描述
我正在使用带有Firebase的飞镖语言创建用于验证的Flutter应用程序.
I am creating a flutter app using dart language with Firebase for authentication.
我为用户注册的代码是
Future<void> signUp() async {
if(_formKey.currentState.validate()){
_formKey.currentState.save();
try{
AuthResult user = await FirebaseAuth.instance.createUserWithEmailAndPassword(email: _email, password: _password);
Navigator.of(context).pop();
Navigator.pushReplacement(context,MaterialPageRoute(builder: (context)=> LoginPage()));
}catch(e){
print(e.message);
}
有人知道我将如何使用新创建的用户凭据在数据库中为用户创建一个条目吗?
Does anyone know how I would use that newly created users credentials to create an entry in the database for user?
我已经为我手动输入的虚拟用户设置了数据库,但希望能够通过注册用户来创建条目.
I have set up the database with a dummy user which I manually entered but would like to be able to create entries from a user signing up.
推荐答案
您可以从AuthResult获取uid,以在Firestore中创建文档.setData更新文档,或者在这种情况下,创建一个文档,因为它不存在.确保安装cloud_firestore_package.
You can get the uid from AuthResult to create a document in Firestore. setData updates the document or in this case, creates one because it doesn't exist. Make sure to install cloud_firestore_package.
UserCredential result = await FirebaseAuth.instance.createUserWithEmailAndPassword(email: _email, password: _password);
User user = result.user
await FirebaseFirestore.instance.collection('users')
.doc(user.uid).set({ 'firstName': _firstName'})
这篇关于Flutter:使用从Firebase身份验证创建的帐户在Firestore中创建用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!