gpt4 book ai didi

firebase - 如何在 flutter firebase 中解决这个 NoSuchMethodError

转载 作者:IT王子 更新时间:2023-10-29 07:08:00 27 4
gpt4 key购买 nike

我有这段代码应该返回 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com