useWatcher top-level property
Creates a watcher hook that observes changes with fine-grained control.
A watcher provides more control than effects, allowing you to compare old and new values and define custom trigger conditions.
Parameters:
sources: Function that returns the values to watchfn: Callback function executed when sources changekeys: Optional keys for hook memoizationdebug: Optional debug optionsimmediately: Whether to execute the callback immediately (default is false)when: Optional condition function for custom trigger logic
Returns: A Watcher with fine-grained change detection
Example:
Widget build(BuildContext context) {
final count = useSignal(0);
useWatcher(
() => count.value,
(newValue, oldValue) {
print('Count changed from $oldValue to $newValue');
},
when: (new, old) => new > old, // Only when increasing
);
return ElevatedButton(
onPressed: () => count.value++,
child: Text('Count: ${count.value}'),
);
}
Implementation
final useWatcher = JoltWatcherHookCreator._();