watchOnly<R> method

R watchOnly<R>(
  1. BuildContext context,
  2. R selector(
    1. TListenable1,
    2. AsyncSnapshot<T2>
    )
)

Watch all observables for changes.

Whenever any observable notifies of a change, the selector will be called with the latest values of all observables. If the selector returns a different value, the context will be rebuilt.

Returns the value provided by selector.

It is safe to call this method multiple times within the same build method.

Implementation

R watchOnly<R>(
  BuildContext context,
  R Function(TListenable1, AsyncSnapshot<T2>) selector,
) {
  final contextWatch = InheritedContextWatch.of(context);

  final observable1 = contextWatch.getOrCreateObservable(context, $1);
  if (observable1 == null) return selector($1, AsyncSnapshot<T2>.nothing());
  final arg1 = observable1.subscription.callbackArgument as TListenable1;

  final observable2 = contextWatch.getOrCreateObservable<T2>(context, $2)!;
  final arg2 = observable2.subscription.callbackArgument as AsyncSnapshot<T2>;

  final selectedValue = selector(arg1, arg2);
  observable1.watchOnly(
    (arg1) => selector(
      arg1,
      observable2.subscription.callbackArgument as AsyncSnapshot<T2>,
    ),
    selectedValue,
  );
  observable2.watchOnly(
    (arg2) => selector(
      observable1.subscription.callbackArgument as TListenable1,
      arg2,
    ),
    selectedValue,
  );

  return selectedValue;
}