Flutter - Firebase Auth UID registers as Null in Firestore

Viewed 20

I have a code like this:

 try {
    await _auth.createUserWithEmailAndPassword(
      email: _emailController.text,
      password: _passwordController.text,
    );
    _firestore.collection("Users").doc(_auth.currentUser?.uid).set({
      "ID": _auth.currentUser?.uid, // Saved: Null
      "Email": _emailController.text,
      "Phone": _phoneController.text,
      "Name": _nameController.text,
      "Surname": _surnameController.text,
      "Photo": "https://firebasestorage.googleapis.com/v0/b/teen-software-stock-tracking.appspot.com/o/default.jpg?alt=media&token=0eec2e1f-2ccf-4bda-8664-3d3f018621b3",
      "Level": 0,
      "ProductRight": 10,
      "AddedProduct": 0,
      "AccountCreate": FullDate.toString(),
    });
    _auth.signOut();
    Get.offAll(const LoginPage());
  } on FirebaseAuthException catch (e) {
    // ...
  }

My goal is to save a few of the user's information in Firestore. This information includes the user's UID. But when saving it registers as Null in Firestore.

Why could this be? I would be glad if you help. Thanks for your help in advance.

1 Answers

To get the uid or other data of the newly created user try this:

final userCredential = await _auth.createUserWithEmailAndPassword(
  email: _emailController.text,
  password: _passwordController.text,
);

print(userCredential.user.uid);
print(userCredential.user.email);
// etc.

Related