flutter_bloc : Make initialState method async

Issue

I am using the flutter_bloc package to manage state in my app. I have a usecase where I have to load the initial state from the remote DB. This requires the initialState method to be async, which it is not.

If not by using the initialState method, what is the best way to load the initial state of a Bloc from a remote DB ?

Solution

You can send an event to the bloc to start loading(on it event bloc send new LoadingState) where you receive and show Loader, then when loading ended bloc send another `state with data and you just switch loading state to loaded(and show data). You don’t need to await call, what you have to do is just pushing and receiving states

Answered By – Yauhen Sampir

Answer Checked By – Willingham (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.