我有这段代码应该返回 userId。问题是它返回 null 因为用户已注销。
@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 block 之后,这仍然会引发错误。该错误卡住了我的应用程序 错误:
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。
Future<String> currentUser() async {
FirebaseUser user = await _firebaseAuth.currentUser();
return user != null ? user.uid : null;
}
关于firebase - 如何在 flutter firebase 中解决这个 NoSuchMethodError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51951700/