0

On my first screen, I fill out the form and then click the next button it added SubmitDataEvent() to the bloc. Then, the BolcListner listing and when it comes to SuccessSate it navigate to the next screen.

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.

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. So in dispose() I used 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. 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. 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);            }
          },
....
15
  • Where do you create your bloc? Nov 23, 2021 at 9:31
  • Please, Share bloc code snippet.
    – Poran
    Nov 23, 2021 at 9:37
  • I used bloc provider top of the my page Nov 23, 2021 at 9:40
  • Try to use Navigator.pushNamed() or Navigator.pushReplacementNamed() for Navigation Nov 23, 2021 at 9:41
  • @Poran I added code Nov 23, 2021 at 9:48

2 Answers 2

1

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();
  }
0

You can try with this snippet

 if (result.responseCode == APIResponse.RESPONSE_SUCCESS) {
                  yield SubmitPersonalDetailsSuccessState(
                      submitPersonalDetailsResponseEntity: r);
                } else
                    yield SubmitPersonalDetailsFailedState(error: r.responseMsg);
                }
1
  • still not working Nov 23, 2021 at 13:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.