Closed AnsarAzees closed 5 years ago
New persistence feature:
More persistence features:
https://github.com/marcglasberg/async_redux#saving-and-loading
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.
@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).
@marcglasberg Thank you very much, I will try this way on my flutter app.
Your store optionally accepts a list of
stateObservers
, which can be used for persistence:At the moment you have to persist the state yourself, by creating the
MyPersistor
class above, and its methodpersistDifference(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.