无法无条件访问属性“uid”,因为接收者可以为“null”

Posted

技术标签:

【中文标题】无法无条件访问属性“uid”,因为接收者可以为“null”【英文标题】:The property 'uid' can't be unconditionally accessed because the receiver can be '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());

【问题讨论】:

有问题的家伙的回答是否回答了您的问题? 【参考方案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

【讨论】:

以上是关于无法无条件访问属性“uid”,因为接收者可以为“null”的主要内容,如果未能解决你的问题,请参考以下文章

Flutter - '属性'设置'不能无条件访问,因为接收器可以是'null''

无法无条件调用方法“[]”,因为接收者可以为“null”

如何解决“无法无条件调用运算符‘0’,因为接收者可以为‘null’”[重复]

不能无条件调用运算符“*”,因为接收者可以为“null”。尝试向目标添加空检查('!')

snapshot.data.length 中的 Flutter Null 安全错误

I2C 从机发送完数据后,接收不到主机(接收到倒数第二个数据后)发送的停止条件