Arman Habib
Arman Habib

Reputation: 11

The property 'uid' can't be unconditionally accessed because the receiver can be 'null'

User is not working. I'm not uploading a collection in Firebase

try {
  FirebaseAuth.instance.createUserWithEmailAndPassword
    (email: kEmail.text, password: kPassword.text)
      .then((value) {
    userCollection.doc(value.user.uid).set({
      'userName': kUserName,
      'email': kEmail,
      'password': kPassword,
      'uid': value.user.uid
    });
  });
} catch (err) {
  print(err.toString());
}

1

Upvotes: 1

Views: 3450

Answers (2)

Sagnik Ghosh
Sagnik Ghosh

Reputation: 67

change it to like this

(e.data() as dynamic)['myFieldOne']

https://i.sstatic.net/IhA7h.png

Upvotes: 0

Nikhil Badyal
Nikhil Badyal

Reputation: 1697

User can be null. So do null checking first like

try {
    FirebaseAuth.instance
        .createUserWithEmailAndPassword(
            email: kEmail.text, password: kPassword.text)
        .then((value) {
      if(value!=null && value.user != null){
         userCollection.doc(value.user.uid).set({
        'userName': kUserName,
        'email': kEmail,
        'password': kPassword,
        'uid': value.user.uid
         });
      }else{
        throw Error();
       }
      
    });
  } catch (err) {
    print(err.toString);
  }

or a simple sol will be

..
Rest of the code...

userCollection.doc(value.user!.uid)

....Rest of the code //Add ! after user

Upvotes: 1

Related Questions