read<T> abstract method

T read<T>(
  1. RootProvider<Object?, T> provider
)

Read the state associated with a provider, without listening to that provider.

By calling read instead of watch, this will not cause a provider's state to be recreated when the provider obtained changes.

A typical use-case for this method is when passing it to the created object like so:

final configsProvider = FutureProvider(...);
final myServiceProvider = Provider((ref) {
  return MyService(ref.read);
});

class MyService {
  MyService(this.read);

  final Reader read;

  Future<User> fetchUser() {
    // We read the current configurations, but do not care about
    // rebuilding MyService when the configurations changes
    final configs = read(configsProvider.future);

    return dio.get(configs.host);
  }
}

By passing read to an object, this allows our object to read other providers. But we do not want to re-create our object if any of the provider obtained changes. We only want to read their current value without doing anything else.

If possible, avoid using read and prefer watch, which is generally safer to use.

Implementation

T read<T>(RootProvider<Object?, T> provider);