watch function

VoidCallback watch(
  1. List<Listenable> depends,
  2. VoidCallback callback, {
  3. bool immediate = false,
})

Registers a callback to be executed when any of the Listenables in depends change. Optionally, the callback can be invoked immediately.

The callback is wrapped in a oneCallTask to ensure it is only executed once per microtask.

  • depends: A list of Listenables to listen to.
  • callback: The callback to execute when any of the Listenables change.
  • immediate: If true, the callback is called immediately.

Returns a callback that can be used to unregister the listener.

Implementation

VoidCallback watch(List<Listenable> depends, VoidCallback callback,
    {bool immediate = false}) {
  final notifier = Listenable.merge(depends);

  callback = oneCallTask(callback);

  if (immediate) callback();
  notifier.addListener(callback);

  return () => notifier.removeListener(callback);
}