OnBuilder<T>.data constructor

OnBuilder<T>.data({
  1. Key? key,
  2. IObservable<T>? listenTo,
  3. List<IObservable>? listenToMany,
  4. required Widget builder(
    1. T data
    ),
  5. SideEffects<T>? sideEffects,
  6. ShouldRebuild? shouldRebuild,
  7. Object? watch()?,
  8. 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.data({
  Key? key,
  IObservable<T>? listenTo,
  List<IObservable>? listenToMany,
  required Widget Function(T data) builder,
  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, ___) => builder(snap.state),
        sideEffects: sideEffects,
        shouldRebuild: (oldSnap, newSnap) {
          if (StateStatus.hasData == newSnap.status ||
              StateStatus.isIdle == newSnap.status) {
            return shouldRebuild?.call(oldSnap, newSnap) ?? true;
          }
          return false;
        },
      );