-
Notifications
You must be signed in to change notification settings - Fork 215
Asynchronously restoring state when initializing #82
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hey there! This is definitely one way to do it, but perhaps not ideal. Let's start with the error. The problem is you're using FutureBuilder, which will actually run the builder function twice: First, when Overall, if you're going "The Redux Way," I'd recommend putting this "Loading" logic into your Redux flow!
Now all of this logic is in the Store! You can now use your normal StoreConnector Widgets to react to these changes. Hope this helps :) If not, let me know! Btw, you can see this type of thing in the Flutter Architecture Samples redux app: https://github.com/brianegan/flutter_architecture_samples/blob/master/example/redux/lib/middleware/store_todos_middleware.dart#L45 |
Thanks! |
Hi @brianegan , The Epics Stream fetchUserEpic(Stream actions, EpicStore<AppState> store) =>
actions.whereType<FetchLocalUserRequestAction>()
.debounceTime(Duration(seconds: 5))
.switchMap((action) => _fetchUser());
Stream _fetchUser() =>
handleException<AuthResponse, FetchLocalUserResultAction, FetchLocalUserErrorAction>(
api: () => _mergeUser(),
onResult: (result) => FetchLocalUserResultAction(result),
onError: (error) => FetchLocalUserErrorAction(error)
);
Future<AuthResponse> _mergeUser() async {
final token = await SharedPreferenceService.getAccessToken();
final user = await SharedPreferenceService.getUser();
return AuthResponse(token: token, user: user);
}
Stream fetchPreferredLanguageEpic(Stream actions, EpicStore<AppState> store) =>
actions.whereType<FetchPreferredLanguageRequestAction>()
.switchMap((action) => _fetchPreferredLanguage());
Stream _fetchPreferredLanguage() =>
handleException<String, FetchPreferredLanguageResultAction, FetchPreferredLanguageErrorAction>(
api: () => SharedPreferenceService.getLanguageCode(),
onResult: (code) => FetchPreferredLanguageResultAction(code),
onError: (error) => FetchPreferredLanguageErrorAction(error)
);
Stream fetchOnboardingStatusEpic(Stream actions, EpicStore<AppState> store) =>
actions.whereType<FetchOnboardingStatusRequestAction>()
.switchMap((action) => _fetchOnboardingStatus());
Stream _fetchOnboardingStatus() =>
handleException<OnboardingStatus, FetchOnboardingStatusResultAction, FetchOnboardingStatusErrorAction>(
api: () => SharedPreferenceService.getOnboardingStatus(),
onResult: (status) => FetchOnboardingStatusResultAction(status),
onError: (error) => FetchOnboardingStatusErrorAction(error)
); Middleware syncLocalData<State>(
Store<State> store,
action,
NextDispatcher next
){
if(action is SyncLocalDataRequestAction){
final xx = store.dispatch(FetchPreferredLanguageRequestAction());
print('xx: $xx');
store.dispatch(FetchLocalUserRequestAction());
store.dispatch(FetchOnboardingStatusRequestAction());
}
next(action);
} |
I'm pretty new to Flutter/Dart, so please bear with me as I try to explain what I'm trying to do...
My app state needs to be initialized with an object that is restored from shared preferences, if it exists. Basically, when the app first runs the user must select a casino from a list on a landing page. Doing so sets the object on the state and saves the id of the casino to shared preferences. The next time the app starts, it should restore the last viewed casino from preferences to the app state and show the home screen.
Now, I've actually got this working but A) I'm not sure this is the best way to do it and B) a non-fatal error is being thrown as soon as the app starts and I don't understand why.
main.dart
The error
The text was updated successfully, but these errors were encountered: