【问题标题】:I'm getting an error NoSuchMethodError: The method currentUser was called on null.FLUTTER我收到一个错误 NoSuchMethodError: The method currentUser was called on null.FLUTTER
【发布时间】:2019-12-10 06:13:18
【问题描述】:

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

我看到了解决方案here 但我没有正确理解它

当前用户函数:

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) {}
}

【问题讨论】:

    标签: firebase flutter firebase-authentication


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2019-12-05
      • 1970-01-01
      • 2021-12-31
      • 1970-01-01
      • 1970-01-01
      • 2021-08-28
      • 2018-11-08
      • 2020-06-26
      • 2023-03-23
      相关资源
      最近更新 更多