一键导入
asyncredux-actions-no-state-change
// Creates AsyncRedux (Flutter) actions that return null from reduce() to not change the state. Such actions can still do side effects, dispatch other actions, or do nothing.
// Creates AsyncRedux (Flutter) actions that return null from reduce() to not change the state. Such actions can still do side effects, dispatch other actions, or do nothing.
| name | asyncredux-actions-no-state-change |
| description | Creates AsyncRedux (Flutter) actions that return null from reduce() to not change the state. Such actions can still do side effects, dispatch other actions, or do nothing. |
In AsyncRedux, returning a new state from reducers is optional. When you don't need to
modify the application state, return null to keep the current state unchanged.
Return null from reduce() when no state modification is needed:
class MyAction extends ReduxAction<AppState> {
AppState? reduce() {
// Perform side effects here
return null; // State remains unchanged
}
}
Only update state when certain conditions are met:
class GetAmount extends ReduxAction<AppState> {
Future<AppState?> reduce() async {
int amount = await getAmount();
if (amount == 0)
return null; // No change needed
else
return state.copy(counter: state.counter + amount);
}
}
Actions that dispatch other actions but don't modify state directly:
class InitAction extends ReduxAction<AppState> {
AppState? reduce() {
dispatch(ReadDatabaseAction());
dispatch(StartTimersAction());
dispatch(TurnOnListenersAction());
return null; // This action doesn't change state itself
}
}
Call external services without modifying app state:
class SendNotification extends ReduxAction<AppState> {
final String message;
SendNotification(this.message);
Future<AppState?> reduce() async {
await notificationService.send(message);
return null;
}
}
Trigger navigation as a side effect:
class GoToSettings extends ReduxAction<AppState> {
AppState? reduce() {
dispatch(NavigateAction.pushNamed('/settings'));
return null;
}
}
AppState? for sync, Future<AppState?> for asyncURLs from the documentation:
Inject dependencies into actions using the environment, dependencies, and configuration pattern. Covers creating an Environment enum, a Dependencies class, passing them to the Store, accessing them from actions and widgets, and using dependency injection for testability.
Create a custom base action class for your app. Covers adding getter shortcuts to state parts, adding selector methods, implementing shared wrapError logic, and establishing project-wide action conventions.
Initialize, setup and configure AsyncRedux in a Flutter app. Use it whenever starting a new AsyncRedux project, or when the user requests.
Stops an AsyncRedux (Flutter) action from dispatching. Use only when the user mentions abortDispatch(), or explicitly asks to abort or prevent dispatch under certain conditions.
Checks an AsyncRedux (Flutter) action's completion status using ActionStatus right after the dispatch returns. Use only when you need to know whether an action completed, whether it failed with an error, what error it produced, or how to navigate based on success or failure.
Creates AsyncRedux (Flutter) asynchronous actions for API calls, database operations, and other async work.