颤振:考虑在互联网更改其状态时"dispose"取消任何活动工作



当互联网关闭时,我收到以下消息。

E/flutter (26162): [ERROR:flutter/lib/ui/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).
E/flutter (26162): Consider canceling any active work during "dispose" or using the "mounted" getter to determine if the State is still active.

它显示了我代码中这一部分的消息。

@override
void initState() {
super.initState();
try {
InternetAddress.lookup('google.com').then((result) {
if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
// internet conn available
Navigator.of(context).pushReplacement(MaterialPageRoute(
builder: (context) =>
(Constants.prefsMobile.getString("mobile") == null
? Login()
// : SignupPayoutPassword(signupdata: [])),
: Home(signindata: signinData)),
));
} else {
// no conn
_showdialog();
}
}).catchError((error) {
// no conn
_showdialog();
});
} on SocketException catch (_) {
// no internet
_showdialog();
}
Connectivity()
.onConnectivityChanged
.listen((ConnectivityResult connresult) {
if (connresult == ConnectivityResult.none) {
} else if (previous == ConnectivityResult.none) {
// internet conn
Navigator.of(context).pop();
Navigator.of(context).pushReplacement(MaterialPageRoute(
builder: (context) =>
(Constants.prefsMobile.getString("mobile") == null
? Login()
: Home(signindata: signinData)),
));
}
previous = connresult;
});
}

我没有为此使用任何处置方法。如果有人知道,请告诉我如何解决这个问题。如何处置。我收到一个崩溃报告后,我的应用程序关闭如下

E/AndroidRuntime( 8064): java.lang.RuntimeException: Unable to destroy activity {com.example.aa_store/com.example.aa_store.MainActivity}: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter activity

这是针对上述问题的崩溃消息吗?请帮忙。

请使用。

@override
void dispose() {
Connectivity().onConnectivityChanged.cancel();
super.dispose();
}

更好的是,在initState:之外定义您的流

Stream _connectivityStream = Connectivity().onConnectivityChanged;

并且在处置中使用CCD_ 2。

该错误意味着您实例化了一个流,该流在事件更改时触发构建更改。此流是在initState期间设置的,也就是说,当小部件首次创建时。Connectivity().onConnectivityChanged.listen(....etc)

但是,当小部件被释放时,您永远不会告诉flutter取消侦听此流。

这就是dispose方法的作用。类似于您希望在构建小部件时执行逻辑的方式,您使用initState,当您不再对这些逻辑更改感兴趣时,也应该告诉它。

如果不这样做,除了内存泄漏之外,还会导致出现错误。

这是您发布的错误This widget has been unmounted, so the State no longer has a context (and should be considered defunct).的翻译"嘿,这个小部件已经不在树中了,它的状态没有挂载,我无法重建它,你需要注意它。

请考虑对这些Flutter元素使用dispose方法,更不用说所有元素了,但从我的想法来看:

  • 动画控制器
  • 计时器
  • 流式传输侦听器

最新更新