onOrElse<R> method
R
onOrElse<R>({
- R onIdle()?,
- R onWaiting()?,
- R onError(
- dynamic error,
- VoidCallback refreshError
- R onData(
- T data
- required R orElse(
- 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 parametersonIdle
,onWaiting
,onError
andonData
).
- 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);
}