Write asynchronous actions
Override reduce() with Future<AppState?>, or Future<AppState> when every
successful path returns state. Each path returning non-null state must execute an
await in the reducer itself. A path returning null may exit immediately. Use a
synchronous reducer for sync work. If an asynchronous reducer needs to return state on
a path with no I/O, use await microtask on that path. The
checks this rule.
After each await, state may contain changes from other actions. Apply your update
to the current state. Use initialState for the state at dispatch time. If an operation
can become obsolete while awaiting, check that its result still belongs to the current
account, item, or request.
This reducer uses the state and base action from the setup skill.
class FetchAndIncrement extends AppAction {
FetchAndIncrement(this.fetchAmount);
final Future<int> Function() fetchAmount;
@override
Future<AppState?> reduce() async {
final amount = await fetchAmount();
if (amount == 0) return null;
return state.copy(counter: state.counter + amount);
}
}