1

I am using the firebase sdk for flutter to sign up users. The firebase function signInWithEmailAndPassword returns me a userCredential. It contains a user object in it.

userCredential = await _auth.createUserWithEmailAndPassword(
  email: email,
  password: password,
);

final user = userCredential.user;
if (user != null) {
  print(user);                                //  1
  print(user.uid);                            //  2
}

await FirebaseFirestore.instance
    .collection('users')
    .doc(userCredential.user!.uid)            //  3
    .set({
  'username': username,
  'email': email,
});

The above script returned a correct user object:

flutter: User(displayName: null, email: [email protected], emailVerified: false, isAnonymous: false, metadata: UserMetadata(creationTime: 2023-03-21 03:52:37.635Z, lastSignInTime: 2023-03-21 03:52:37.635Z), phoneNumber: null, photoURL: null, providerData, [UserInfo(displayName: null, email: [email protected], phoneNumber: null, photoURL: null, providerId: password, uid: [email protected])], refreshToken: , tenantId: null, uid: d3wI96PxFKSHGiXb8CIVlIJtC0r2)

however, on line 2, the getter uid returned me a null, and caused error on line 3.

I am expecting uid getter returns me a String so that I can use it to store user's information on firestore.

1 Answer 1

0

You may try this to get the userId

FirebaseAuth.instance.currentUser!.uid;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks but I tried it and it still does not work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.