如何解决类型错误:无法读取空颤振的属性“uid”
我目前在登录时遇到此错误消息,我的注册功能运行良好。但似乎无法弄清楚为什么我的登录名不断出现此错误。如果我能指出我的错误来自哪里,我将不胜感激。
void loginUser() async {
User user;
await _auth
.signInWithEmailAndPassword(email: email.text,password: password.text)
.then((value) {
setState(() {
loading = false;
});
return value;
}).catchError((error) {
Navigator.pop(context);
Get.defaultDialog(title: 'Error in Login');
});
if (user != null) {
readData(user).then((value) => Get.toNamed(homeRoute));
}
}
Future readData(User user) async {
FirebaseFirestore.instance
.collection('users')
.doc(user.uid)
.get()
.then((result) async {
await App.sharedPreferences.setString('uid',result.get('uid'));
await App.sharedPreferences.setString('email',result.get('email'));
await App.sharedPreferences.setString('fullname',result.get('fullname'));
await App.sharedPreferences.setString('bvn',result.get('bvn'));
});
}
这是我的注册功能
void _registerUser() async {
User user;
await _auth
.createUserWithEmailAndPassword(
email: email.text,password: password.text)
.then((value) {
user = value.user;
}).catchError((error) {
Navigator.pop(context);
Get.defaultDialog(title: 'Error in registration');
});
if (user != null) {
saveToFirestore(user).then((value) => Get.toNamed(homeRoute));
}
}
Future saveToFirestore(User user) async {
FirebaseFirestore.instance.collection('users').doc(user.uid).set({
'uid': user.uid,'fullname': fullname.text.trim(),'email': user.email,'bvn': bvn.text
});
await App.sharedPreferences.setString('uid',user.uid);
await App.sharedPreferences.setString('email',user.email);
await App.sharedPreferences.setString('fullname',fullname.text);
await App.sharedPreferences.setString('bvn',bvn.text);
}
解决方法
改变
User user;
await _auth
到User user = await _auth....
在您的 FireStore 查询中,使用
Future readData(User user) async {
FirebaseFirestore.instance
.collection('users')
.doc(user.user.uid) // instead of user.uid
.get()
所有带有 user.uid
的地方都将其更改为 user.user.uid
。