Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
386 views
in Technique[技术] by (71.8m points)

how to persist redux app state in flutter?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...