Flutter:当互联网改变其状态时,考虑取消“处置”期间的任何活动工作

Aji*_*tta 3 dispose flutter

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

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.
Run Code Online (Sandbox Code Playgroud)

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

@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;
    });
  }
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

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

Hut*_*yad 6

请用。

@override
  void dispose() {
    Connectivity().onConnectivityChanged.cancel();
    super.dispose();
  }
Run Code Online (Sandbox Code Playgroud)

更好的是,在以下位置定义您的流initState

Stream _connectivityStream = Connectivity().onConnectivityChanged;
Run Code Online (Sandbox Code Playgroud)

并在废弃后使用_connectivityStream.cancel();

该错误意味着您实例化了一个流,该流在事件发生变化时会触发构建更改。该流是在 期间设置的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方法,更不用说所有元素,但从我的想法来看:

  • 动画控制器。
  • 计时器。
  • 串流听众。