如何在扑扑的firebase中解决此NoSuchMethodError
问题描述:
我有应该返回用户ID的这段代码.问题是,由于用户已注销,它返回null.
I have this code which is supposed to return the userId. Problem is it returns null since the user is signed out.
@override
void initState() {
// TODO: implement initState
super.initState();
try {
widget.auth.currentUser().then((userId) {
setState(() {
authStatus = userId == null ? AuthStatus.notSignedIn : AuthStatus.signedIn;
});
});
} catch (e) {}
}
即使将catch包缠在它周围,这仍然会引发错误.该错误冻结了我的应用程序 错误:
This still throws an error even after wrapping a catch block around it. the error freezes my app Error:
Exception has occurred.
NoSuchMethodError: The getter 'uid' was called on null.
Receiver: null
Tried calling: uid
尝试调用的方法是
Future<String> currentUser() async {
FirebaseUser user = await _firebaseAuth.currentUser();
return user.uid;
}
答
尝试一下:
widget.auth.currentUser().then((userId) {
setState(() {
authStatus = userId == null ? AuthStatus.notSignedIn : AuthStatus.signedIn;
});
}).catchError((onError){
authStatus = AuthStatus.notSignedIn;
});
更新 如果firebaseAuth返回null,则不能使用用户的uid属性,因为它为null.
Update If the firebaseAuth return null you can't use uid property from user because it's null.
Future<String> currentUser() async {
FirebaseUser user = await _firebaseAuth.currentUser();
return user != null ? user.uid : null;
}