whenData<R> method

AsyncValue<R> whenData<R>(
  1. R cb(
    1. T value
    )
)

Shorthand for when to handle only the data case.

For loading/error cases, creates a new AsyncValue with the corresponding generic type while preserving the error/stacktrace.

Implementation

AsyncValue<R> whenData<R>(R Function(T value) cb) {
  return _map(
    data: (d) {
      try {
        return AsyncValue.data(cb(d.value));
      } catch (err, stack) {
        return AsyncValue.error(err, stackTrace: stack);
      }
    },
    error: (e) => AsyncError(e.error, stackTrace: e.stackTrace),
    loading: (l) => AsyncLoading<R>(),
  );
}