dart 无法无条件访问属性“uid”,因为receiver可以为null

llew8vvj  于 2022-12-28  发布在  其他
关注(0)|答案(1)|浏览(158)

我试图返回值在用户中,我得到这个错误,如果我试图通过添加null符号修复这个错误,它不会返回任何值,因为所有这些值将为null,我想返回这个值并保存它,这样我就可以使用它,什么可以是解决方案,请指导

FirebaseAuth auth = FirebaseAuth.instance;
User? user = FirebaseAuth.instance.currentUser;
CollectionReference users = FirebaseFirestore.instance.collection('users');

Future<void> addUser(context) {
  //before we add user we need to check this user already signedin earlier using same number or not
  //if signedIn before no need to add , because it may have added already. so just need to skip adduser



  return users
      .add({
    'uid': user.uid,
    'mobile':  user.phoneNumber,
    'email': user.email
  })
      .then((value){
        //after add data to firebase then will go to next screen
    //if you pushreplacementName or push replacement, then u cant go back to previous screen
    Navigator.pushReplacementNamed(context, LocationScreen.id);

  })
      .catchError((error) => print("Failed to add user: $error"));
}
u3r8eeie

u3r8eeie1#

这是因为uid可以为空。请尝试以下操作

return users
      .add({
    'uid': user.uid ?? "default username"
    'mobile':  user.phoneNumber,
    'email': user.email
  })

如果uid为空,默认用户名将保存在firestore中。

相关问题