mutateAsync<T> method

  1. @protected
Future<void> mutateAsync<T>({
  1. required Future<T> action(),
  2. required void apply(
    1. T result
    ),
  3. void onError(
    1. Object error,
    2. StackTrace stack
    )?,
  4. String? label,
})

Asynchronously executes action, then passes the result to apply inside mutate if successful.

If an error occurs during action, calls onError if provided; otherwise records the error and rethrows.

Implementation

@protected
Future<void> mutateAsync<T>({
  required Future<T> Function() action,
  required void Function(T result) apply,
  void Function(Object error, StackTrace stack)? onError,
  String? label,
}) async {
  if (_disposed) return;
  T result;
  try {
    result = await action();
  } catch (e, st) {
    if (onError != null) {
      onError(e, st);
    } else {
      _onMutationError(e, st, label);
      rethrow;
    }
    return;
  }

  if (_disposed) return;
  try {
    mutate(() => apply(result), label: label);
  } catch (e, st) {
    if (onError != null) {
      onError(e, st);
    } else {
      rethrow;
    }
  }
}