watchEffect method
void
watchEffect(
- BuildContext context,
- void effect(
- AsyncSnapshot<
T1> , - AsyncSnapshot<
T2> , - TListenable3
- AsyncSnapshot<
- Object? key,
- bool immediate = false,
- bool once = false,
Watch all observables for changes.
Whenever any observable notifies of a change, the effect
will be
called with the latest values of all observables, without rebuilding the widget.
Conditional effects are supported, but it's highly recommended to specify
a unique key
for all such effects followed by the unwatchEffect call
when condition is no longer met:
if (condition) {
(listenable, stream, future).watchEffect(context, key: 'effect', (_, _, _) {...});
} else {
(listenable, stream, future).unwatchEffect(context, key: 'effect');
}
If immediate
is true
, the effect will be called upon effect
registration immediately. If once
is true
, the effect will be called
only once. These parameters can be combined.
immediate
and once
parameters require a unique key
.
Implementation
void watchEffect(
BuildContext context,
void Function(AsyncSnapshot<T1>, AsyncSnapshot<T2>, TListenable3) effect, {
Object? key,
bool immediate = false,
bool once = false,
}) {
final contextWatch = InheritedContextWatch.of(context);
final obs1 = contextWatch.getOrCreateObservable<T1>(context, $1);
if (obs1 == null) return;
final obs2 = contextWatch.getOrCreateObservable<T2>(context, $2)!;
final obs3 = contextWatch.getOrCreateObservable(context, $3)!;
obs1.watchEffect((arg1) {
if (shouldAbortMassEffect3(key, obs1, obs2, obs3,
once: once, immediate: immediate)) {
return;
}
final arg2 = obs2.subscription.callbackArgument as AsyncSnapshot<T2>;
final arg3 = obs3.subscription.callbackArgument as TListenable3;
effect(arg1, arg2, arg3);
}, key: key, immediate: immediate, once: once);
obs2.watchEffect((arg2) {
if (shouldAbortMassEffect3(key, obs1, obs2, obs3,
once: once, immediate: immediate)) {
return;
}
final arg1 = obs1.subscription.callbackArgument as AsyncSnapshot<T1>;
final arg3 = obs3.subscription.callbackArgument as TListenable3;
effect(arg1, arg2, arg3);
}, key: key, immediate: immediate, once: once);
obs3.watchEffect((arg3) {
if (shouldAbortMassEffect3(key, obs1, obs2, obs3,
once: once, immediate: immediate)) {
return;
}
final arg1 = obs1.subscription.callbackArgument as AsyncSnapshot<T1>;
final arg2 = obs2.subscription.callbackArgument as AsyncSnapshot<T2>;
effect(arg1, arg2, arg3);
}, key: key, immediate: immediate, once: once);
}