Bloc: Flutter Back Navigation

Created on 19 Jun 2020  路  2Comments  路  Source: felangel/bloc

Hi,

I'm starting to using Bloc, and it is really awesome congrats. This is not bug, is a question on how to use it properly.

Scenario
Widget 1 has Bloc 1
Events: LoadData, GoNext
States: Initial, Loading, Loaded, Next

The GoNext event yields the Next event to navigate to the Widget 2.

Widget 2 has Bloc 2
States: Initial, Loading, Finish

If the user is on the Widget 2 and tries to go back to the Widget 1, the Widget 1 maintains the last state: Next.

So if the user tries to go to the Widget 2 again, this action doesn't work because there is no change of states (the onStateChange does not get triggered as expected)

How should I implement this kind of scenario?

Example
Lets say the users writes answers for 10 questions on the Widget 1 then goes to the Widget 2 to complete more questions. If the users goes back to the Widget 1, the questions are all filled up.

What I'm trying to say here is that, I can't reset to an Initial state, the Widget 1 has to maintain the state.

Thanks,
William

question

Most helpful comment

Hi @gfiury 馃憢

You should not extend equatable on your Next state. That will ensure that each yield Next() will be considered a new state and will be emitted. You can then set up a BlocListener and handle the navigation accordingly. If you're also looking to keep the loaded data then you should either copy the existing data to Next state or have the Loaded state containing info that would trigger the navigation besides holding the actual data.
As a side note, are you sure you need the Next state ? You might be able to directly navigate to your 2nd widget without having to route it though bloc.

Hope that helps! 馃憤

All 2 comments

Hi @gfiury 馃憢

You should not extend equatable on your Next state. That will ensure that each yield Next() will be considered a new state and will be emitted. You can then set up a BlocListener and handle the navigation accordingly. If you're also looking to keep the loaded data then you should either copy the existing data to Next state or have the Loaded state containing info that would trigger the navigation besides holding the actual data.
As a side note, are you sure you need the Next state ? You might be able to directly navigate to your 2nd widget without having to route it though bloc.

Hope that helps! 馃憤

Hi @RollyPeres,

Thanks for the quick reply!!!

"You should not extend equatableon your Next state". You are right, I did that and It is working as I expected. Also the next state has all the data as you said.

Are you sure you need the Next state ?
I'm using the Bloc to validate the info before navigating to the next Widget. The user press Next, and if everything is valid, the Next state is yield. If the info is not valid ErrorInfo is yield with the correspondent message.

ErrorInfo is not in the scenario I presented, my bad.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

krusek picture krusek  路  3Comments

timtraversy picture timtraversy  路  3Comments

nhwilly picture nhwilly  路  3Comments

MahdiPishguy picture MahdiPishguy  路  3Comments

shawnchan2014 picture shawnchan2014  路  3Comments