I try to store users inside collection in Firestore as documents and want to make every User ID is same as Document ID.. I tried but it still gives me another ID for the user:
Future<void> userSetupDone() async {
CollectionReference users = FirebaseFirestore.instance.collection('Users');
final docUser = FirebaseFirestore.instance.collection('Users').doc();
FirebaseAuth auth = FirebaseAuth.instance;
String? uid = docUser.id;
// String? uid = auth.currentUser?.uid.toString();
String? email = auth.currentUser?.email.toString();
String? phone = auth.currentUser?.phoneNumber.toString();
String? displayName = auth.currentUser?.displayName.toString();
users.add({'Uid': uid, "Email": email, "Phone": phone, "Name": displayName});
return;
}
You have to update uid immediately after adding data to the database. Because you can not get id before adding that data from the Firestore Database.
You will get it by following code:
Future<void> userSetupDone() async {
CollectionReference users = FirebaseFirestore.instance.collection('Users');
final docUser = FirebaseFirestore.instance.collection('Users').doc();
FirebaseAuth auth = FirebaseAuth.instance;
String? email = auth.currentUser?.email.toString();
String? phone = auth.currentUser?.phoneNumber.toString();
String? displayName = auth.currentUser?.displayName.toString();
DocumentReference reference= await users.add({'Uid': '', "Email": email, "Phone": phone, "Name": displayName});
await reference.update({"Uid": reference.id});
return;
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments