when my user sign up, he puts his email, username and password. The email and password are stored in the Firebase Auth database. So an Id is created and associated for this user. (kmupeDXnfEYnhbdpkvLUoFbIjnb2 for exemple). Now I want to create an user profile (profile pic, username, etc..). For that I have to create a Firestore collection and document with the username and the ID linked to the firebase auth (kmupeDXnfEYnhbdpkvLUoFbIjnb2). I have no idea how to do that can someone help me ? Thanks a lot !
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:flutter_dev/reusable.dart';
import 'package:flutter_dev/home.dart';
class SignUp extends StatefulWidget {
const SignUp({super.key});
@override
State<SignUp> createState() => _SignUpState();
}
class _SignUpState extends State<SignUp> {
final TextEditingController _passwordTextController = TextEditingController();
final TextEditingController _emailTextController = TextEditingController();
final TextEditingController _userNameTextController = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomInset: false,
extendBodyBehindAppBar: true,
appBar: AppBar(
backgroundColor: Colors.transparent,
elevation: 0,
title: const Text('Sign Up'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const SizedBox(
height: 20,
),
reusableTextField("Enter Username", Icons.person_outline, false,
_userNameTextController, context),
const SizedBox(
height: 20,
),
reusableTextField("Enter Email ID", Icons.email_outlined, false,
_emailTextController, context),
const SizedBox(
height: 20,
),
reusableTextField("Enter Password", Icons.lock_outline, true,
_passwordTextController, context),
const SizedBox(
height: 20,
),
signInsignUpButton(context, false, () {
final db = FirebaseFirestore.instance;
db.collection("users").doc("uid").set({
'fullName': _userNameTextController,
'email': _emailTextController,
'accountCreated': Timestamp.now(),
}).onError((e, _) => print("Error writing document: $e"));
FirebaseAuth.instance
.createUserWithEmailAndPassword(
email: _emailTextController.text,
password: _passwordTextController.text)
.then((value) {
print("Created New Account");
Navigator.pushReplacement(context,
MaterialPageRoute(builder: (context) => const Home()));
}).onError((error, stackTrace) {
print("Error ${error.toString()}");
});
}),
],
),
),
);
}
}
CodePudding user response:
I believe this would do what you want:
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:flutter_dev/reusable.dart';
import 'package:flutter_dev/home.dart';
class SignUp extends StatefulWidget {
const SignUp({super.key});
@override
State<SignUp> createState() => _SignUpState();
}
class _SignUpState extends State<SignUp> {
final TextEditingController _passwordTextController = TextEditingController();
final TextEditingController _emailTextController = TextEditingController();
final TextEditingController _userNameTextController = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomInset: false,
extendBodyBehindAppBar: true,
appBar: AppBar(
backgroundColor: Colors.transparent,
elevation: 0,
title: const Text('Sign Up'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const SizedBox(
height: 20,
),
reusableTextField("Enter Username", Icons.person_outline, false,
_userNameTextController, context),
const SizedBox(
height: 20,
),
reusableTextField("Enter Email ID", Icons.email_outlined, false,
_emailTextController, context),
const SizedBox(
height: 20,
),
reusableTextField("Enter Password", Icons.lock_outline, true,
_passwordTextController, context),
const SizedBox(
height: 20,
),
signInsignUpButton(context, false, () {
FirebaseAuth.instance
.createUserWithEmailAndPassword(
email: _emailTextController.text,
password: _passwordTextController.text)
.then((value) {
final db = FirebaseFirestore.instance;
db.collection("users").doc("uid/${value.user.uid}").set({
'fullName': _userNameTextController.text,
'email': _emailTextController.text,
'accountCreated': Timestamp.now(),
}).onError((e, _) => print("Error writing document: $e"));
print("Created New Account");
Navigator.pushReplacement(context,
MaterialPageRoute(builder: (context) => const Home()));
}).onError((error, stackTrace) {
print("Error ${error.toString()}");
});
}),
],
),
),
);
}
}
The FirebaseAuth.instance.createUserWithEmailAndPassword(
callback is of type UserCredential
. You can access the uid
via this UserCredential
: value.user.uid