update method

  1. @protected
Future<State> update(
  1. FutureOr<State> cb(
    1. State
    ), {
  2. FutureOr<State> onError(
    1. Object err,
    2. StackTrace stackTrace
    )?,
})
inherited

A function to update state from its previous value, while abstracting loading/error cases for state.

If state was in error state, the callback will not be invoked and instead the error will be returned. Alternatively, onError can specified to gracefully handle error states.

See also:

  • future, for manually awaiting the resolution of state.

Implementation

@protected
Future<State> update(
  FutureOr<State> Function(State) cb, {
  FutureOr<State> Function(Object err, StackTrace stackTrace)? onError,
}) async {
  // TODO cancel on rebuild?

  final newState = await future.then(cb, onError: onError);
  state = AsyncData<State>(newState);
  return newState;
}