Flutter: Creating a user in Firestore using the account created from Firebase Authentication
5,768
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})
Author by
callum norris
Updated on December 18, 2022Comments
-
callum norris over 1 year
I am creating a flutter app using dart language with Firebase for authentication.
My code for a user signing up is
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.
-
callum norris about 4 yearsThanks for the very helpful answer!! For anyone else reading this, I added the package & installed it. Then needed to enable multidexing link You can use this line of code for adding multiple values to the one document as per below code:
code
await Firestore.instance.collection('users').document(user.uid).setData({ 'firstName': _firstName, 'lastName': _lastName, 'userName': _userName}) -
adamcapjones over 2 yearsresult.user is returning an error: LateInitializationError: Field 'user; has not been initialized.