whenOrElse<R> method

R whenOrElse<R>({
  1. R onLoading()?,
  2. R onDone(
    1. T? data,
    2. bool isLoadingMore
    )?,
  3. R onError(
    1. Object? error
    )?,
  4. R onInit()?,
  5. required R orElse()?,
})

Declare methods that return widgets base on fetchingStatus

Allow to use orElse, skip some methods that you don't need.

Implementation

R whenOrElse<R>({
  R Function()? onLoading,
  R Function(T? data, bool isLoadingMore)? onDone,
  R Function(Object? error)? onError,
  R Function()? onInit,
  required R Function()? orElse,
}) {
  switch (fetchingStatus) {
    case LoadState.init:
      return onInit == null ? orElse!() : onInit();
    case LoadState.loading:
      return onLoading == null ? orElse!() : onLoading();
    case LoadState.loadingMore:
    case LoadState.done:
      return onDone == null ? orElse!() : onDone(data, isLoadingMore);
    case LoadState.error:
      return onError == null ? orElse!() : onError(error);
    default:
      return orElse!();
  }
}