maybeWhen<R> method

R maybeWhen<R>({
  1. R data(
    1. T data
    )?,
  2. R error(
    1. Object error,
    2. StackTrace? stackTrace
    )?,
  3. R loading()?,
  4. required R orElse(),
})

Switch-case over the state of the AsyncValue while purposefully not handling some cases.

If AsyncValue was in a case that is not handled, will return orElse.

Implementation

R maybeWhen<R>({
  R Function(T data)? data,
  R Function(Object error, StackTrace? stackTrace)? error,
  R Function()? loading,
  required R Function() orElse,
}) {
  return _map(
    data: (d) {
      if (data != null) return data(d.value);
      return orElse();
    },
    error: (e) {
      if (error != null) return error(e.error, e.stackTrace);
      return orElse();
    },
    loading: (l) {
      if (loading != null) return loading();
      return orElse();
    },
  );
}