watch<T> method

T watch<T>()

Obtain a value from the nearest ancestor provider of type T, and subscribe to the provider.

Calling this method is equivalent to calling:

Provider.of<T>(context)

This method is accessible only inside StatelessWidget.build and State.build.
If you need to use it outside of these methods, consider using Provider.of instead, which doesn't have this restriction.
The only exception to this rule is Providers's update method.

See also:

  • ReadContext and its read method, similar to watch, but doesn't make widgets rebuild if the value obtained changes.

Implementation

T watch<T>() {
  return Provider.of<T>(this);
}