onOrElse<R> method

R onOrElse<R>({
  1. R onIdle()?,
  2. R onWaiting()?,
  3. R onError(
    1. dynamic error,
    2. VoidCallback refreshError
    )?,
  4. R onData(
    1. T data
    )?,
  5. required R orElse(
    1. T data
    ),
})

Listen to the injected Model and rebuild when it emits a notification.

  • Required parameters:
    • builder Default callback (called in replacement of any non defined optional parameters onIdle, onWaiting, onError and onData).
  • Optional parameters:
    • onIdle : callback to be executed when injected model is in its initial state.
    • onWaiting : callback to be executed when injected model is in waiting state.
    • onError : callback to be executed when injected model has error.
    • onData : callback to be executed when injected model has data.
    • initState : callback to be executed when the widget is first inserted into the widget tree.
    • dispose : callback to be executed when the widget is removed from the widget tree.
    • shouldRebuild : Callback to determine whether this StateBuilder will rebuild or not.
    • watch : callback to be executed before notifying listeners. It the returned value is the same as the last one, the rebuild process is interrupted.
    • onSetState :For side effects before rebuilding the widget tree.
    • onAfterBuild :For side effects after rebuilding the widget tree.
    • debugPrintWhenRebuild : Print state transition log.

Implementation

R onOrElse<R>({
  R Function()? onIdle,
  R Function()? onWaiting,
  R Function(dynamic error, VoidCallback refreshError)? onError,
  R Function(T data)? onData,
  required R Function(T data) orElse,
}) {
  if (isIdle && onIdle != null) {
    return onIdle();
  }
  if (isWaiting && onWaiting != null) {
    return onWaiting();
  }
  if (hasError && onError != null) {
    return onError(snapError!.error, snapError!.refresher);
  }
  if (hasData && onData != null) {
    return onData(data as T);
  }
  return orElse(data as T);
}