OnBuilder<T>.orElse constructor

OnBuilder<T>.orElse({
  1. Key? key,
  2. IObservable<T>? listenTo,
  3. List<IObservable>? listenToMany,
  4. Widget onIdle()?,
  5. Widget onWaiting()?,
  6. Widget onError(
    1. dynamic error,
    2. void refreshError()
    )?,
  7. Widget onData(
    1. T data
    )?,
  8. required Widget orElse(
    1. T data
    ),
  9. SideEffects<T>? sideEffects,
  10. ShouldRebuild? shouldRebuild,
  11. Object? watch()?,
  12. String? debugPrintWhenRebuild,
})

Explicitly listenTo one or more injected state and reinvoke its onBuilder callback each time an injected state emits a notification.

For each OnBuilder widget flavor there is method like equivalent:

//Widget-like
OnBuilder(
    listenTo: counter,
    builder: () => Text('${counter.state}'),
),

//Method-like
counter.rebuild(
    () => Text('{counter.state}'),
),
//
//Widget-like
OnBuilder.data(
    listenTo: counter,
    builder: (data) => Text('$data')),
),

//Method-like
counter.rebuild.onData(
    (data) => Text(data),
),

//Widget-like
OnBuilder.all(
    listenTo: counter,
    onIdle: () => Text('onIdle'),
    onWaiting: () => Text('onWaiting'),
    onError: (err, errorRefresh) => Text('onError'),
    onData: (data) => Text('$data'),

)

//Method-like
counter.rebuild.onAll(
    onIdle: () => Text('onIdle'),
    onWaiting: () => Text('onWaiting'),
    onError: (err, errorRefresh) => Text('onError'),
    onData: (data) => Text('$data'),
),
//
//Widget-like
OnBuilder.orElse(
    listenTo: counter,
    onWaiting: () => Text('onWaiting'),
    orElse: (_) => Text('{counter.state}'),

),

//Method-like
counter.rebuild.onOrElse(
    onWaiting: () => Text('onWaiting'),
    orElse: (_) => Text('{counter.state}'),
),

Implementation

OnBuilder.orElse({
  Key? key,
  IObservable<T>? listenTo,
  List<IObservable>? listenToMany,
  Widget Function()? onIdle,
  Widget Function()? onWaiting,
  Widget Function(dynamic error, void Function() refreshError)? onError,
  Widget Function(T data)? onData,
  required Widget Function(T data) orElse,
  SideEffects<T>? sideEffects,
  ShouldRebuild? shouldRebuild,
  Object? Function()? watch,
  String? debugPrintWhenRebuild,
})  : assert(listenTo != null || listenToMany != null),
      super(
        key: key,
        observers: (_) => listenTo != null
            ? [listenTo as ReactiveModelImp]
            : listenToMany!.cast<ReactiveModelImp>(),
        builder: (_, snap, ___) {
          return snap.onOrElse<Widget>(
            onIdle: onIdle,
            onWaiting: onWaiting,
            onError: onError,
            onData: onData != null ? (_) => onData(_) : null,
            orElse: (_) => orElse(_),
          );
        },
        sideEffects: sideEffects,
        shouldRebuild: shouldRebuild,
      );