watch method

T watch(
  1. BuildContext context
)

Implementation

T watch(BuildContext context) {
  final elementRef = WeakReference(context as Element);
  final listenerWrapper = _ListenerWrapper();
  listenerWrapper.listener = () {
    assert(
      SchedulerBinding.instance.schedulerPhase != SchedulerPhase.persistentCallbacks,
      """
          Do not mutate state (by setting the value of the ValueNotifier
          that you are subscribed to) during a `build` method. If you need
          to schedule a value update after `build` has completed, use
          `SchedulerBinding.instance.scheduleTask(updateTask, Priority.idle)`,
          `SchedulerBinding.addPostFrameCallback(updateTask)`, '
        or similar.
        """,
    );
    // If the element has not been garbage collected (causing
    // `elementRef.target` to be null), or unmounted
    if (elementRef.target?.mounted ?? false) {
      // Mark the element as needing to be rebuilt
      elementRef.target!.markNeedsBuild();
    }
    // Remove the listener -- only listen to one change per `build`
    valueNotifier.removeListener(listenerWrapper.listener!);
  };
  valueNotifier.addListener(listenerWrapper.listener!);
  return valueNotifier.value;
}