![](/img/trans.png)
[英]Flutter Unhandled Exception: This widget has been unmounted, so the State no longer has a context
[英]Flutter error: Unhandled Exception: This widget has been unmounted, so the State no longer has a context
我正在使用authStateChanges().listen
来查看用户何时登录或注销以及应用程序应将它们带到何处。 但由于某种原因,我收到以下错误。
E/flutter (29194): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: This widget has been unmounted, so the State no longer has a context (and should be considered defunct).
E/flutter (29194): Consider canceling any active work during "dispose" or using the "mounted" getter to determine if the State is still active.
我需要if (user == null)
成为Navigator.pushAndRemoveUntil
因为我需要从另一个页面触发dispose()
FirebaseAuth.instance.authStateChanges().listen((User? user) {
UserModel loggedInUser = UserModel();
User? user = FirebaseAuth.instance.currentUser;
// if (mounted) {
if (user == null) {
print(
'------------------------------------------------------User is currently signed out!');
print(user);
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(
builder: (BuildContext context) => const LoginScreen()),
(Route<dynamic> route) => false);
} else {
print(user);
print(
'--------------------------------------------------User is signed in!');
Navigator.of(context).pushAndRemoveUntil(MaterialPageRoute(builder: (context) =>
const loadData()), (Route<dynamic> route) => false);
}
//}
});
您收到错误是因为您使用的上下文属于不再存在的State
(猜测它只是一个普通的有状态页面并且存在时间很短)。
您正在尝试做的是路由但没有上下文。 您可以使用GlobalKey来实现这一点,
然后你可以在main.dart
之类的地方注册回调监听器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.