如何在扑扑的firebase中解决此NoSuchMethodError [英] How to resolve this NoSuchMethodError in flutter firebase
本文介绍了如何在扑扑的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;
}
这篇关于如何在扑扑的firebase中解决此NoSuchMethodError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文