-
Notifications
You must be signed in to change notification settings - Fork 215
Add example to OnWillChange docs #75
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
OnWillChange happens before the Widget is rendered due to a change in the store (or view model if you've set it up to limit the update count). Your view model will be the latest version, but your widget tree will still be the previously rendered version. This is a perfect place to navigate to a new page if your view model is looks a certain way or you could use it to post a snack bar in response to your change. It would look something like this: onWillChange: (viewModel) {
if (viewModel.wasSuccessful) {
Scaffold.of(context).showSnackBar(SnackBar( content: Text("Action was Successful!")));
}
} The didChange callback is called when the widget tree is still volatile. Your buildContext by this point won't be fresh enough to do something like use Navigator. This was a lesson i learned the hard way, when I tried to integrate router functionalities into my store. I can't tell by your question, if you are having troubles with something refreshing when it shouldn't. It kind of seemed like that was an original problem that led to your current problem. If that is the case, be sure you've set up your StoreConnectors to only refresh each widget by adding overrides to the == operator and the get hashCode for the view models you want to limit your updates with, using the distinct flag. |
@brianegan Is there a way to compare the old and new viewmodel? Or old and new store? if (viewModel.wasSuccessful && !oldViewModel.wasSuccessful) {
// show SnackBar
} would be better I think. Or how do you handle one time property changes like this? |
In React, I used to avoid diffing states altogether by simply having my actions return a promise and then chaining a Either way, I'd love to know if anyone has found a workaround for showing the snack bar just once. EDIT: // auth_actions.dart
Future<String> loginAction(Store<AppState> store) async {
store.dispatch(IsLoadingAction(true));
try {
GoogleSignInAccount _currentUser = await _googleSignIn.signIn();
GoogleSignInAuthentication _googleAuth = await _currentUser.authentication;
store.dispatch(LoginAction(_currentUser, _googleAuth));
store.dispatch(NavigateToAction.pushNamedAndRemoveUntil(
'/home',
(Route<dynamic> route) => false,
));
store.dispatch(IsLoadingAction(false));
return '';
} catch (error) {
store.dispatch(AuthError(error.toString()));
store.dispatch(IsLoadingAction(false));
return error.toString();
}
} // signin.dart
/* ... */
GoogleSignInButton(
onPressed: () async {
String error = await vm.login();
if (error != '') {
Scaffold.of(context).showSnackBar(SnackBar(content: Text(error)));
}
},
),
/* ... */ I still dispatched my |
OnWillChange now supports new and old viewmodel, also adde example to latest version. |
flutter_redux/lib/flutter_redux.dart
Line 105 in dfc42c3
is there any example of using this ?
I got stressed Out since my TabBar always rebuild after dispatching a filter. Since then, the TabController animation which shows the current tab index will never be shown
The text was updated successfully, but these errors were encountered: