Search code examples
flutterblocflutter-bloc

flutter_bloc : Make initialState method async


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

  • Instead of making async use two states i.e loading and loaded

    Explanation:

    • You can send an event to the bloc to start loading (on it event bloc send new LoadingState) where you receive and show Loader,

    • As soon as loading ends , 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