首页 > 解决方案 > 我收到一个错误 NoSuchMethodError: The method currentUser was called on null.FLUTTER

问题描述

我正在尝试检查用户是否已登录,我已使用 firebase 验证了我的颤振应用程序,我希望用户的状态将应用程序重定向到登录页面或主页,但我无法运行该应用程序在红屏上显示错误,错误是:NoSuchMethodError:方法'currentUser'被调用为null。接收方:null 尝试调用:currentUser()

我在这里看到了解决方案, 但我没有正确理解

当前用户功能:

Future<String> currentUser() async {
  FirebaseUser user = await _firebaseAuth.currentUser();
  return user!= null ? user.uid : null;
}

检查身份验证状态:

void initState() {
super.initState();
  try {
    widget.auth.currentUser().then((userId) {
    setState(() {
    authStatus =
    userId == null ? AuthStatus.notsignedIn : AuthStatus.signedIn;
   });
  });
} catch (e) {}
}

标签: firebaseflutterfirebase-authentication

解决方案


该错误仅表示您的_firebaseAuth对象为空。尝试使用

FirebaseUser user = await FirebaseAuth.instance.currentUser();

推荐阅读