watch<T> method Null safety

T watch<T>()

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

If T is nullable and no matching providers are found, watch will return null. Otherwise if T is non-nullable, will throw ProviderNotFoundException. If T is non-nullable and the provider obtained returned null, will throw ProviderNullException.

This allows widgets to optionally depend on a provider:

  Builder(builder: (context) {
    final value =<Movie?>();

    if (value == null) Text('no Movie found');
    return Text(movie.title);

Calling this method is equivalent to calling:


This method is accessible only inside and
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.


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