简体   繁体   English

Flutter 再次触发 bloc 事件

[英]Flutter Trigger bloc event again

On my first screen, I fill out the form and then click the next button it added SubmitDataEvent() to the bloc.在我的第一个屏幕上,我填写了表格,然后单击下一个按钮,它将SubmitDataEvent()添加到该集团。 Then, the BolcListner listing and when it comes to SuccessSate it navigate to the next screen.然后,BolcListner 列表和SuccessSate导航到下一个屏幕。

on the second screen, when I click the back button it navigates to the previous screen.在第二个屏幕上,当我单击后退按钮时,它会导航到上一个屏幕。 After that, when I change the user-input data on the form and again click the next button now SubmitDataEvent() is not added.之后,当我更改表单上的用户输入数据并再次单击下一步按钮时,现在未添加SubmitDataEvent()

I preferred some resources related to this and I understand the problem is that the state is in SuccessSate and it doesn't change to InitialState .我更喜欢与此相关的一些资源,我理解问题是 state 处于SuccessSate并且它不会更改为InitialState So in dispose() I used bloc.close();所以在dispose()我使用bloc.close();

@override
  void dispose() {
    bloc.close();
    super.dispose();
  }

But still, it's not working.但是,它仍然无法正常工作。 Also, I try with this code另外,我尝试使用此代码

@override
  void dispose() {
    bloc.emit(InitialState);
    bloc.close();
    super.dispose();
  }

still, it's not working.仍然,它不起作用。

I used this to navigate between screens:我用它在屏幕之间导航:

Navigator.popAndPushNamed()

What I want to do is: On the first screen, when clicking on the next button SubmitDataEvent() added to the bloc and it in SuccessState it navigate to the next screen.我想要做的是:在第一个屏幕上,当单击添加到块中的下一个按钮SubmitDataEvent()时,它在SuccessState中导航到下一个屏幕。 When I click the back button on the second page it navigates again to the first screen.当我单击第二页上的后退按钮时,它会再次导航到第一个屏幕。 Now when I click the next button on the first screen I want to run all bloc process again.现在,当我单击第一个屏幕上的下一个按钮时,我想再次运行所有 bloc 进程。 There are no dependencies with the first and second screens.第一个和第二个屏幕没有依赖关系。

first screen code:第一个屏幕代码:

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

    bloc = injection<SubmitPersonalDetailsBloc>();

    EasyLoading.addStatusCallback((status) {
      print('EasyLoading Status $status');
      if (status == EasyLoadingStatus.dismiss) {
        _timer?.cancel();
      }
    });
  }

 @override
  void dispose() {
    _scrollController.dispose();
    bloc.close();
    super.dispose();
  }
 @override
  Widget buildView(BuildContext context) {
    return Scaffold(
      body: BlocProvider<SubmitPersonalDetailsBloc>(
        create: (_) => bloc,
        child: BlocListener<SubmitPersonalDetailsBloc,
            BaseState<PersonalDetailsState>>(
          listener: (context, state) {
            if (state is LoadingSubmitPersonalDetailsState) {
              EasyLoading.show(status: 'Submitting Data');
            }
            if (state is SubmitPersonalDetailsSuccessState) {
              setState(() {
                submitPersonalDetailsResponseEntity =
                    state.submitPersonalDetailsResponseEntity;
              });
              if (submitPersonalDetailsResponseEntity!.responseCode == "00") {
                EasyLoading.showSuccess('Done!');
                //Navigate next screen
                EasyLoading.dismiss();
              }
            } else if (state is SubmitPersonalDetailsFailedState) {
              EasyLoading.showError(state.error);            }
          },
....

You can try with this snippet您可以尝试使用此代码段

 if (result.responseCode == APIResponse.RESPONSE_SUCCESS) {
                  yield SubmitPersonalDetailsSuccessState(
                      submitPersonalDetailsResponseEntity: r);
                } else
                    yield SubmitPersonalDetailsFailedState(error: r.responseMsg);
                }

The problem is on Dependency Injection, Once it creates an instance the parameters don't change.问题在于依赖注入,一旦它创建了一个实例,参数就不会改变。 So when navigating to the next screen have to reset that instance.因此,当导航到下一个屏幕时,必须重置该实例。

@override
  void dispose() {
    _scrollController.dispose();
    bloc.close();
    injection.resetLazySingleton<SubmitPersonalDetailsBloc>(); // here reset the instance
    super.dispose();
  }

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

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