简体   繁体   English

Flutter:来自后退导航的回调产生“错误的 state:调用关闭后无法添加新事件”

[英]Flutter : Callback from back navigation produces “Bad state: Cannot add new events after calling close”

I have a list page (stateful widget) that is a list of subsidiaries of a firm and a separate editing page (stateful widget) for editing and removing specific subsidiary.我有一个列表页面(有状态小部件),它是公司子公司的列表,还有一个单独的编辑页面(有状态小部件),用于编辑和删除特定子公司。 Every row of the list is a widget which has a button that takes user to the editing page where he can also remove the corresponding subsidiary.列表的每一行都是一个小部件,它有一个按钮,可以将用户带到编辑页面,在那里他还可以删除相应的子公司。 Below is the code of this button下面是这个按钮的代码

          FlatButton(
                  onPressed: () => Navigator.of(context).push(MaterialPageRoute(
                        builder: (context) =>
                            BlocProvider<SubsidiaryEditBloc>(
                                 builder: (BuildContext context) => SubsidiaryEditBloc(),
                                 child: SubsidiaryEditPage(subsidiary: subsidiary)
                             ) 
                            )).then((value) {
                              subsidiaryListBloc.dispatch(Reset()); 
                            }),
                  child: Text(
                    "Edit",
                  ),
                )

When editing/removal is done the user navigates back, and I want the list to reflect the update/removal.完成编辑/删除后,用户导航回来,我希望列表反映更新/删除。 To achieve this I use the "then" callback on the "push" action where I dispatch "Reset" event on the bloc attached to the list page which makes bloc fetch subsidiaries from repository as if they were requested for the first time.为了实现这一点,我在“push”操作上使用“then”回调,在该操作中我在附加到列表页面的 bloc 上调度“Reset”事件,这使得 bloc 从存储库中获取子公司,就好像它们是第一次被请求一样。 However it does not quite work out.然而,它并不完全奏效。 When a subsidiary is edited, saved, and the user navigates back, nothing bad happens, the list is successfully updated.当一个子公司被编辑、保存并且用户导航回来时,没有任何不好的事情发生,列表被成功更新。 But when a subsidiary is removed, navigating back yields error mentioned in the subject.但是,当子公司被删除时,导航返回会产生主题中提到的错误。 I am new to flutter and don't yet know all the pitfalls nor clearly understand the good and the bad practices.我是 flutter 的新手,还不知道所有的陷阱,也不清楚好的和坏的做法。 Is this callback a clear source of this error?这个回调是这个错误的明确来源吗? I assume that it is bound to specific instance of subsidiary widget so that when list elements change, the widget and bloc are disposed - is that correct?我假设它绑定到附属小部件的特定实例,以便当列表元素更改时,小部件和块被处置 - 对吗? Or am I not allowed to dispatch any bloc events on navigation for some other reason?或者由于其他原因,我是否不允许在导航上发送任何 bloc 事件?

Using the "then" keyword usually doesn't work.使用“then”关键字通常不起作用。 I use await before the navigator so it will await the page to be popped and then run you dispatch code!我在导航器之前使用等待,所以它会等待页面被弹出,然后运行你调度代码!

Try this:尝试这个:

 FlatButton(
  onPressed: () async{
   await Navigator.of(context).push(MaterialPageRoute(
       builder: (context) =>
          BlocProvider<SubsidiaryEditBloc>(
                builder: (BuildContext context) => SubsidiaryEditBloc(),
                child: SubsidiaryEditPage(subsidiary: subsidiary)
            ) 
          ));
    subsidiaryListBloc.dispatch(Reset()); 
     },

              child: Text(
                "Edit",
              ),
            )

暂无
暂无

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

相关问题 StateError(错误状态:调用关闭后无法添加新事件) - StateError (Bad state: Cannot add new events after calling close) 错误:错误状态:调用关闭后无法添加新事件 - Error: Bad state: Cannot add new events after calling close flutter:未处理的异常:错误 state:调用关闭后无法添加新事件 - flutter: Unhandled Exception: Bad state: Cannot add new events after calling close 错误 State:调用关闭后无法添加新事件 - Flutter 模块化 - Bad State: Cannot add new events after calling close - Flutter Modular Flutter:未处理的异常:错误状态:调用close后无法添加新事件(不一样的情况) - Flutter: Unhandled Exception: Bad state: Cannot add new events after calling close (NOT SAME CASE) 未处理的异常:错误 state:调用关闭后无法添加新事件 - 屏幕之间 - Unhandled Exception: Bad state: Cannot add new events after calling close - Between screens 未处理的异常:错误 state:调用关闭音频播放器后无法添加新事件 - Unhandled Exception: Bad state: Cannot add new events after calling close Audio player 未处理的异常:错误 state:调用关闭后无法添加新事件? - Unhandled Exception: Bad state: Cannot add new events after calling close? 未处理的异常:错误 state:在使用 fluttter_google_places 调用关闭后无法添加新事件 - Unhandled Exception: Bad state: Cannot add new events after calling close, on using fluttter_google_places Flutter bloc Cubit Bad state:调用关闭后无法发出新状态 - Flutter bloc Cubit Bad state: Cannot emit new states after calling close
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM