maybeWhen<R> method

R maybeWhen<R>({
  1. bool skipLoadingOnReload = false,
  2. bool skipLoadingOnRefresh = true,
  3. bool skipError = false,
  4. R data(
    1. T data
    )?,
  5. R error(
    1. Object error,
    2. StackTrace stackTrace
    )?,
  6. R loading()?,
  7. 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.

By default, when skips "loading" states if triggered by a Ref.refresh or Ref.invalidate (but does not skip loading states if triggered by Ref.watch).

In the event that an AsyncValue is in multiple states at once (such as when reloading a provider or emitting an error after a valid data), when offers various flags to customize whether it should call loading/error/data :

  • skipLoadingOnReload (false by default) customizes whether loading should be invoked if a provider rebuilds because of Ref.watch. In that situation, when will try to invoke either error/data with the previous state.

  • skipLoadingOnRefresh (true by default) controls whether loading should be invoked if a provider rebuilds because of Ref.refresh or Ref.invalidate. In that situation, when will try to invoke either error/data with the previous state.

  • skipError (false by default) decides whether to invoke data instead of error if a previous value is available.

Implementation

R maybeWhen<R>({
  bool skipLoadingOnReload = false,
  bool skipLoadingOnRefresh = true,
  bool skipError = false,
  R Function(T data)? data,
  R Function(Object error, StackTrace stackTrace)? error,
  R Function()? loading,
  required R Function() orElse,
}) {
  return when(
    skipError: skipError,
    skipLoadingOnRefresh: skipLoadingOnRefresh,
    skipLoadingOnReload: skipLoadingOnReload,
    data: data ?? (_) => orElse(),
    error: error ?? (err, stack) => orElse(),
    loading: loading ?? () => orElse(),
  );
}