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

标签 firebase flutter

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

相关文章:

firebase - 从深层链接打开时如何刷新应用程序?

java - Firebase - 远程配置应用程序更新

javascript - 将时间戳与 00 :00:00 of today? 进行比较的 Firebase 数据库规则

dart - 将数据/消息从 flutter 应用程序传递到其他应用程序

gridview - 脚手架主体中的多个Gridview构建器

flutter - 如何在鼠标悬停时缩放列表中的项目,使其始终可见(Web 平台上的 Flutter)

angular - map 运算符不适用于 AngularFireAuth Observable

android - 单击进入聊天室时应用程序崩溃

android - 使用共享首选项保存列表时遇到问题(仍然需要帮助)

dart - Android 键盘导致状态栏重新出现