marcglasberg / async_redux

Flutter Package: A Redux version tailored for Flutter, which is easy to learn, to use, to test, and has no boilerplate. Allows for both sync and async reducers.
Other
234 stars 40 forks source link

How can we use redux persistence with async redux #33

Closed AnsarAzees closed 5 years ago

marcglasberg commented 5 years ago

Your store optionally accepts a list of stateObservers, which can be used for persistence:

var store = Store<AppState>(
    initialState: state,
    stateObservers: [MyPersistor()],
);

class MyPersistor extends StateObserver<AppState> {

  AppState previousState;

  void observe(
     ReduxAction<St> action, 
     AppState stateIni, 
     AppState stateEnd, 
     int dispatchCount,
     ) { 
     // Compare stateEnd to previousState and persist the difference.
     persistDifference(previousState, stateEnd);
     previousState = stateEnd;
    }
}

At the moment you have to persist the state yourself, by creating the MyPersistor class above, and its method persistDifference(previousState, stateEnd).

Or, if you can wait, I believe I'll be adding out-of-the-box persisting capabilities to AsyncRedux within the next 3 months.

I also believe that https://pub.dev/packages/redux_persist and https://pub.dev/packages/redux_persist_flutter can easily be adapted to work with AsyncRedux.

marcglasberg commented 5 years ago

New persistence feature:

https://github.com/marcglasberg/async_redux#persistence

marcglasberg commented 5 years ago

More persistence features:

https://github.com/marcglasberg/async_redux#saving-and-loading

chachaxw commented 3 years ago

More persistence features:

https://github.com/marcglasberg/async_redux#saving-and-loading

I have a question about the LocalPersist issue, here's an example what I met on theses days, I have a user list and I stored it on local though LocalPersist, but I want to load the user list when the app launch, how can I do that? And another question is that I tried to load the user list when the app init the state, but it didn't work. Do you have any best practices for this part?. I'll be pleasure if you can help me with this, thanks.

marcglasberg commented 3 years ago

@chachaxw

class Business {

  static late Store<AppState> store;

  static late Persistor persistor;

  Future<void> init() async {
    User? firebaseUser = getFirebaseUser();
    persistor = createPersistor();
    AppState state;

    if (firebaseUser == null) {
      state = await resetState();
    }
    else {
      AppState? _state = await persistor.readState();

      var currentUser = readState?.currentUser;

      if (currentUser == null) {
        _state = await resetState();
      } else {
        bool ifUserSeemsCorrect = (currentUser.uid == firebaseUser.uid);
        if (!ifUserSeemsCorrect) {
          _state = await resetState();
        }
      }

      state = _state!;
    }

    store = Store<AppState>(
      initialState: state,
      wrapError: wrapError(),
      persistor: persistor,
    );
  turnOnFirebaseListeners();
  }

  @protected
  WrapError? wrapError() => MyWrapError();

  @protected
  User? getFirebaseUser() => ...

  @protected
  Persistor createPersistor() => MyPersistor();

  /// Deletes all persistence, and saves an initialState.
  @protected
  Future<AppState> resetaState() async {
    await persistor.deleteState();
    AppState state = AppState.initialState();
    await persistor.saveInitialState(state);
    return state;
  }

  void turnOnFirebaseListeners() {
    if (CurrentUser.exists) {
      store.dispatch(TurnOnFirebaseListenersAction());
    }
  }
}

Your MyPersistor class will use LocalPersist (or any other means) to write and read the information. The above code is a suggestion on how to wire things, assuming you are using Firebase (but it's similar for other auth providers).

chachaxw commented 3 years ago

@marcglasberg Thank you very much, I will try this way on my flutter app.