watch<T> static method

T watch<T>(
  1. GetT<T> get, {
  2. bool watching = true,
  3. bool autoVsync = true,
  4. bool useScope = true,
})

This hook function watches a Get object and triggers a rebuild when it sends a notification.

Must be called inside a HookWidget.build method.

Notifications are not sent when watching is false (changes to this value will apply the next time the HookWidget is built).

If a GetVsync object is passed, this hook will check if the Vsync is attached to a BuildContext (which is typically achieved via Ref.vsync) and throws an error if it fails. The check can be bypassed by setting checkVsync to false.

By default, if an ancestor GetScope overrides the Get object's value, the new object is used instead. Setting useScope to false will ignore any overrides.

See also:

  • Ref.select, which allows rebuilding only when a specified part of the listenable's value changes.
  • GetScope.of, for retrieving an Override's new value outside of a HookWidget.build method.

Implementation

static T watch<T>(
  GetT<T> get, {
  bool watching = true,
  bool autoVsync = true,
  bool useScope = true,
}) {
  if (useScope) get = GetScope.of(useContext(), get);
  if (autoVsync && get is GetVsync<T, Animation<T>>) Ref.vsync(get);
  return useValueListenable(get, watching: watching);
}