首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >popAndPushNamed -未处理的异常:此小部件已被卸载,因此状态不再具有上下文(并且应该被视为失效)

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

Stack Overflow用户
提问于 2022-02-07 10:45:55
回答 1查看 378关注 0票数 1

我简要地解释了我的问题,当我在我的应用程序中创建和命名“东西”时,我让760毫秒的时间传递到另一个页面。但是,问题是,在这段时间内,如果我使用popAndPushNamed)的后退按钮退出,我会得到以下错误(插入popAndPushNamed)的函数是使用popAndPushNamed)的函数)

代码语言:javascript
运行
复制
[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.

这是我的代码:

代码语言:javascript
运行
复制
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,
        },
      );
    });
  }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-02-07 10:50:19

使用mounted getter确定状态是否仍处于活动状态

代码语言:javascript
运行
复制
    Future.delayed(const Duration(milliseconds: 730), () {
     if(mounted)
      Navigator.of(context).popAndPushNamed(
        PageNames.something,
        arguments: {
          "info": null,
        },
      );
    });
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71017234

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档