Mackintosh
Mackintosh

Reputation: 11

firebase user.uid returns null although user is returned corrently

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.

Upvotes: 1

Views: 110

Answers (1)

Neil-NotNeo
Neil-NotNeo

Reputation: 986

You may try this to get the userId

FirebaseAuth.instance.currentUser!.uid;

Upvotes: 0

Related Questions