Proper approach with flutter bloc and state initializing

Issue

I have multiple flutter blocs/cubit in my application for example authentication_bloc, user_bloc, theme_bloc, and so on. In every bloc, there is a common state that is initializing state for example AuthInitializingState, UserInitializingState, or ThemeInitializingState… So in main.dart I have to check for every initializing state to show a splash/loading screen. if authState is AuthInitializingState || userState is UserInitializing state || .....{//show splash/loading screen} my question is – is there any better way to accomplish this task?

Solution

If 3 different bloc’s states result in same state, then most probably you might handling the state wrong.

AuthState should be the parent of all the states, in most cases, based on the auth state, the UserState and ThemeState gets affected. What we usually should do is having the AuthState as a global state and only if the AuthBloc emits SuccessState, we usually instantiate UserBloc. That way, the UserBloc starts with initial state.

In your case, I think, it would be better to have ThemeBloc and AuthBloc as global blocs and UserBloc as child bloc if Auth state succeeds.

Hope it helps!

Answered By – BHARATH T

Answer Checked By – David Goodson (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *