繁体   English   中英

popAndPushNamed - 未处理的异常:此小部件已被卸载,因此状态不再具有上下文(应视为已失效)

[英]popAndPushNamed - Unhandled Exception: This widget has been unmounted, so the State no longer has a context (and should be considered defunct)

我简要解释了我的问题,当我在我的应用程序中创建并命名“某物”时,我让 760 毫秒传递到另一个页面。 但是问题是,在那段时间里,如果我使用 AppBar 的后退按钮退出,我会收到以下错误(指示onTitleSelectionAtCreate的函数是我使用popAndPushNamed的函数)

[ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: This widget has been unmounted, so the State no longer has a context (and should be considered defunct).
E/flutter (28951): Consider canceling any active work during "dispose" or using the "mounted" getter to determine if the State is still active.

在此处输入图像描述

这是我的代码:

class InitialRemarkInfo extends StatefulWidget {
  final bool? isCreated;

  const InitialRemarkInfo({
    Key? key,
    required this.isCreated,
  }) : super(key: key);

  @override
  _InitialRemarkInfoState createState() => _InitialRemarkInfoState();
}

class _InitialRemarkInfoState extends State<InitialRemarkInfo>
    with TickerProviderStateMixin {

  double _height = 0;
  double _width = 0;
  bool _resized = false;

  @override
  void initState() {
    super.initState();

    if (!this.widget.isCreated!)
      BlocProvider.of<SomethingBloc>(context).add(InitializeEvent());
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: this._onPopBack,
      child: Scaffold(
        // * APP BAR
        appBar: AppBar(
          title: Row(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              Text(this.localizedString('newSomething')!),
            ],
          ),
          leading: IconButton(
            splashRadius: 1,
            icon: Icon(Icons.arrow_back),
            onPressed: _onPopBack,
          ),
          backgroundColor: DesignConstants.darkBlueBackground,
        ),

        body: BlocConsumer<SomethingBloc, SomethingState>(
          // * LISTENER
          listener: (context, state) async {

            [...]

          },
          builder: (context, state) {
            // * LOADED
            if (state is SomethingLoaded)
              return Stack(
                children: [
                  SingleChildScrollView(
                    physics: AlwaysScrollableScrollPhysics(),
                    child: Column(
                      children: [
                        // * GROWING SPACE (to simulate animation)
                        new AnimatedSize(
                          curve: Curves.easeIn,
                          child: new Container(
                            width: _width,
                            height: _height,
                            color: Colors.transparent,
                          ),
                          duration: new Duration(milliseconds: 700),
                        ),

                        [...]

                        // * TITLE
                        DataRo(
                          title: 'title',
                          icon: Icons.arrow_drop_down,
                          isCreation: true,
                          onTitleSelectionAtCreate: () =>
                              _onTitleSelectionAtCreate(), // Function
                        ),
                      ],
                    ),
                  ),
                ],
              );

            // * DEFAULT
            return Container();
          },
        ),
      ),
    );
  }

  //============================================================================
  // ON POP BACK
  //
  Future<bool> _onPopBack() async {
    BlocProvider.of<SomethingBloc>(context).add(
        ReloadList(isCreation: true, isSaving: false));

    return false;
  }


  // ==========================================================================
  // ON TITLE SELECTION AT CREATE
  //
  void _onTitleSelectionAtCreate() {
      setState(() {
        if (_resized) {
          this._resized = false;
          this._height = 0;
          this._width = 0;
        } else {
          this._resized = true;
          this._height = Device.screenHeight / 3;
          this._width = Device.screenWidth;
        }
      }); // To animate something not relevant
    
    Future.delayed(const Duration(milliseconds: 730), () {
      Navigator.of(context).popAndPushNamed(
        PageNames.something,
        arguments: {
          "info": null,
        },
      );
    });
  }
}

使用mounted的 getter 来确定 State 是否仍然处于活动状态

    Future.delayed(const Duration(milliseconds: 730), () {
     if(mounted)
      Navigator.of(context).popAndPushNamed(
        PageNames.something,
        arguments: {
          "info": null,
        },
      );
    });

Creo que este enlace podría ayudarle

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM