update method
- @visibleForTesting
- @protected
- FutureOr<
State> cb(- State
- FutureOr<
State> onError(- Object err,
- 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 ofstate
.- 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;
}