skip to Main Content

I am new to bloc and flutter bloc.

In my project, apart from other screens I have also 3 screens for user registration.
On the second screen I have a countdown timer implemented in bloc.
When the user goes to the third screen, the state is maintained. When I move back to the second screen the timer is still active.

However when I go (pop) from the second screen to the first screen and then go to the second screen again, the state is lost.

I understand that this happens because I provide the state only in the second screen using BlocProvider() along with BlocBuilder() so each time I pop the screen and then put it back in the stack, the bloc is rebuilt.

The only solution I see, by searching, is to provide the bloc globally at the top of my widget tree (above the materialapp).
I do not find this sensible as I want to have the bloc only in 3 screens, but on the other hand I was wondering if in the end this is a common practice.

My question is, in general, how do we maintain the state when navigating from one screen to the other.

And more broader, what if I have a bloc that I want to access from different, unrelated screens and maintain the state?

Is providing the bloc globally a good practice?
If I scope multiple blocs globally, would this affect my app’s performance and start up time?

2

Answers


  1. My rule of thumb when working with Bloc is: put the BlocProvider as down below in the widget tree as possible.
    When in doubt, I put on top of the widget tree.
    I understand this might look confusing, but it’s best practice, recommended form the devs themself.

    To answer your questions:

    And more broader, what if I have a bloc that I want to access from different, unrelated screens and maintain the state?

    Again, you put it on top of the widget tree, so that your entire app can access it.

    Is providing the bloc globally a good practice?

    Yes, it is.

    If I scope multiple blocs globally, would this affect my app’s performance and start up time?

    That depends on what your code does when it initializes the Bloc, so you definitely have control on over this.

    Login or Signup to reply.
  2. My question is, in general, how do we maintain the state when
    navigating from one screen to the other.

    In your scenario, you should have 1 Global Bloc for 3 screens for common information these screens shared, more specifically in this case is the countdown timer value.

    But for best practice, you should also have 3 different Blocs for each screen to handle their own UI logic and state.

    I have implemented this for many scenarios where the screen needs to share some UI or a screen with many steps, each step has its own screen and I can say that the easiest way to code and maintain with Bloc

    And more broader, what if I have a bloc that I want to access from
    different, unrelated screens and maintain the state?

    You can create a Global Bloc on top of your widget tree. For example when I create a Flutter App, I alway have a ApplicationBloc on top of my widget app, then I can access it from anywhere with BuildContext

    Is providing the bloc globally a good practice? If I scope multiple
    blocs globally, would this affect my app’s performance and start up
    time?

    Create multiple Blocs globally doesn’t affect to your app’s performance, it depends on how you handle with BlocBuilder and BlocListener to rebuild the widget

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search