update method

  1. @visibleForTesting
  2. @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.

This method neither causes state to go back to "loading" while the operation is pending. Neither does it cause state to go to error state if the operation fails.

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.
  • AsyncValue.guard, and alternate way to perform asynchronous operations.

Implementation

@visibleForTesting
@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;
}