【问题标题】:The property 'uid' can't be unconditionally accessed because the receiver can be 'null'无法无条件访问属性“uid”,因为接收者可以为“null”
【发布时间】:2021-07-13 10:20:36
【问题描述】:

用户不工作。我没有在 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());
}

【问题讨论】:

  • 有问题的家伙的回答是否回答了您的问题?

标签: flutter google-cloud-firestore firebase-authentication flutter-layout flutter-dependencies


【解决方案1】:

用户可以为空。所以先做空检查

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);
  }

或者一个简单的溶胶将是

..
Rest of the code...

userCollection.doc(value.user!.uid)

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

【讨论】:

    【解决方案2】:

    改成这样

    (e.data() 作为动态的)['myFieldOne']

    https://i.stack.imgur.com/IhA7h.png

    【讨论】:

      猜你喜欢
      • 2021-09-21
      • 2022-01-11
      • 2022-08-15
      • 2021-08-05
      • 2022-08-15
      • 2021-09-21
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      相关资源
      最近更新 更多