i would like to persist my app states. when i exit the app and i come back to it all my states remain to the initial state. here is my code :
static Future<Map<String, dynamic>> initHome() async {
User user = await User.loadUserDataFromStorage();
await user.refreshData();
User usr = await User.loadUserDataFromStorage();
List<Transaction> userTransactions =
await Transaction.getAllUserTransactions(usr);
List<ProductCategory> productCategories =
await ProductCategory.getAllProductCategories();
List<OperationType> operationsTypes =
await OperationType.getAllOperationTypes();
List<MmpataNotification> notifications =
await MmpataNotification.getUserNotifications(usr.userId);
List<MmpataNotification> notifications2 =
await MmpataNotification.getAllUserNotifications(usr.userId);
List<Subscription> subscriptions =
await Subscription.getAllUserSubscriptions(usr.account.accountId);
List<Product> products = await Product.getAllProducts();
return <String, dynamic>{
"user": usr,
"userTransactions": userTransactions,
"productsCategories": productCategories,
"operationsTypes": operationsTypes,
"notifications": notifications,
"notifications2": notifications2,
"subscriptions": subscriptions,
"products": products,
};
}
then the dispactched method inithome
with redux
initHome: () async {
Map<String, dynamic> homeVars = await RestClient.initHome();
store.dispatch(InitHomeAction(data: homeVars));
},
the reducer :
if (action is InitHomeAction) {
return state.copyWith(
user: action.data['user'],
userTransactions: action.data['userTransactions'],
isHomePageLoading: false,
userTransactionsLoaded: true,
userDataLoaded: true,
isHomeloaded: true,
productsCategories: action.data['productsCategories'],
operationTypes: action.data['operationsTypes'],
notifications: action.data['notifications'],
notifications2: action.data['notifications2'],
subscription: action.data['subscriptions'],
product: action.data['products'],
loggedOut: (action.data['user'].userId > 0) ? false : true,
);
}
and finally the store launching :
final store = Store<MMpataState>(
MMpataReducer.reducer,
initialState: MMpataState.initialState(),
);
runApp(MMpataWalletApp(store: store));
the initialState
method returns the initial State of all my states.
i also tried to use the flutter persist redux package but i could not use the JsonSerialize
property because the fromJson
is not really explicit so i did not success to use this package
so is there a way to better do it or there's a package which can better help me or can you show me how can i better use the redux_persist_flutter
package?
question from:
https://stackoverflow.com/questions/65935715/how-to-persist-redux-app-state-in-flutter