Flutter + Firebase Throwing Unhandled Exception: This widget has been unmounted Error Using Navigato
Posted
技术标签:
【中文标题】Flutter + Firebase Throwing Unhandled Exception: This widget has been unmounted Error Using Navigator to change view【英文标题】: 【发布时间】:2021-06-27 23:52:00 【问题描述】:我有一个由 firebase 驱动的 Flutter 应用程序,该应用程序正在运行,但出现错误。
正如我所料,当应用程序打开时,用户必须使用电子邮件和密码登录,如果登录成功,他们会看到一个名为 WelcomeScreen 的新视图。
发生了什么,用户在登录后被带到欢迎屏幕,但我收到以下错误
[VERBOSE-2:ui_dart_state.cc(186)] Unhandled Exception: This widget has been unmounted, so the State no longer has a context (and should be considered defunct).
Consider canceling any active work during "dispose" or using the "mounted" getter to determine if the State is still active.
#0 State.context.<anonymous closure> (package:flutter/src/widgets/framework.dart:1088:9)
#1 State.context (package:flutter/src/widgets/framework.dart:1094:6)
#2 _AuthScreenState.initState.<anonymous closure> (package:autos_latinos/screens/auth_screen.dart:81:35)
#3 _rootRunUnary (dart:async/zone.dart:1362:47)
#4 _CustomZone.runUnary (dart:async/zone.dart:1265:19)
#5 _CustomZone.runUnaryGuarded (dart:async/zone.dart:1170:7)
#6 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:341:11)
#7 _DelayedData.perform (dart:async/stream_impl.dart:591:14)
#8 _StreamImplEvents.handleNext (dart:async/stream_impl.dart:706:11)
#9 _PendingEvents.schedule.<anonymous closur<…>
它似乎指向我的代码中的这一部分,我用它来跟踪来自 firebase 的 authStateChanges:
FirebaseAuth.instance.authStateChanges().listen((User user)
if (user == null)
print('User is currently signed out!');
if(AuthScreen.id != '/AuthScreen')
Navigator.pushReplacement(context, MaterialPageRoute(builder: (BuildContext context) => AuthScreen()));
else
print("Already on auth screen");
else
//Send to welcome screen
print('User is signed in!');
Navigator.pushReplacement(context, MaterialPageRoute(builder: (BuildContext context) => WelcomeScreen()));
);
非常感谢任何有关如何防止此错误的建议。
【问题讨论】:
您好,您找到解决方案了吗?我需要在我的代码中解决这个问题。 【参考方案1】:您必须像这样在有状态小部件的dispose
方法中配置监听器:
class Sample extends StatefulWidget
@override
_SampleState createState() => _SampleState();
class _SampleState extends State<Sample>
StreamSubscription<User> _listener;
@override
Widget build(BuildContext context)
return Container();
@override
void initState()
_listener = FirebaseAuth.instance.authStateChanges().listen((User user)
if (user == null)
print('User is currently signed out!');
if (AuthScreen.id != '/AuthScreen')
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (BuildContext context) => AuthScreen()));
else
print("Already on auth screen");
else
//Send to welcome screen
print('User is signed in!');
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (BuildContext context) => WelcomeScreen()));
);
super.initState();
@override
void dispose()
_listener.cancel();
super.dispose();
【讨论】:
我试了一下,但它仍然给我以下错误[VERBOSE-2:ui_dart_state.cc(186)] Unhandled Exception: setState() called after dispose(): _LoginCardState#bf03a(lifecycle state: defunct, not mounted, tickers: tracking 0 tickers)
以上是关于Flutter + Firebase Throwing Unhandled Exception: This widget has been unmounted Error Using Navigato的主要内容,如果未能解决你的问题,请参考以下文章
Flutter 和 Firebase:任务 ':firebase_auth:compileDebugJavaWithJavac' 执行失败
没有创建 Firebase 应用“[DEFAULT]” - 在 Flutter 和 Firebase 中调用 Firebase.initializeApp()
用于 Flutter 桌面嵌入的 Firebase 身份验证插件