Abhi Singh
Abhi Singh

Reputation: 217

Can i store application data using bloc

I have used redux in react-native and so when moving flutter i am learning bloc. I know about the working of bloc in basic like how to handle API call and manage state of the widget in flutter.Now i want to know if i can store application of the whole data like list of products in cart with bloc.Something like this.

void main() => runApp(
    BlocProvider<ApplicationBloc>(
      bloc: ApplicationBloc(),
      child: MyApp(),
    )
);

Other screen will have there own bloc but this application bloc will store data related to whole app. Is this correct or not or to store application data like user data do i have to store in shared preferences?

Upvotes: 1

Views: 2943

Answers (1)

rexloxrix
rexloxrix

Reputation: 61

One way is to save data in shared preferences as you said and emit a corresponding bloc event to see if data is stored in shared preferences.

Another way is to use the hydrated bloc package, which allows you to automatically save the state of a bloc, and upon restart of the app, the hydrated bloc automatically restores it's state which was saved. This is for example useful if you want to save if the user enabled black theme and thus to restore the app with the black theme.

You can read further about hydrated bloc here: hydrated bloc

Upvotes: 3

Related Questions