mapValue<R> method

AsyncValue<R> mapValue<R>(
  1. MapCallback<T, R> callback
)

Maps the underlying value using the given callback function

Implementation

AsyncValue<R> mapValue<R>(MapCallback<T, R> callback) {
  AsyncValue<R>? prev;

  if (hasValue) {
    prev = AsyncData<R>(callback(value as T));
  }

  if (hasError) {
    final next = AsyncError<R>(error!, stackTrace ?? StackTrace.current);
    prev = prev != null ? next.copyWithPrevious(prev) : next;
  }

  if (isLoading) {
    prev = prev != null
        ? AsyncLoading<R>().copyWithPrevious(prev)
        : AsyncLoading<R>();
  }

  if (prev != null) {
    return prev;
  } else {
    // No data, no error, not loading. Should be impossible to reach
    return AsyncLoading<R>();
  }
}