robotoaster
robotoaster

Reputation: 3132

How to listen to changes on ProviderContainer in Riverpod?

In a context of pure Dart project I want to "watch" changes on dependencies. How do I listen to the changes?

As far as read() I am all fine i guess.

final container = ProviderContainer();
final dependency = container.read(myDependencyProvider);

Upvotes: 2

Views: 3207

Answers (2)

Maks
Maks

Reputation: 7955

Actually per Remi's answer on the issue in Github, the new, updated recommended way to watch for changes outside of Flutter, is to use the listen() method on the ProviderContainer like so:

final provider = Provider((ref) => ...)

void main() {
  final container = ProviderContainer();

  container.listen(
    provider,
    (previous, value) => print('changed'),
  ); 
}

Upvotes: 2

Mohammad Alotol
Mohammad Alotol

Reputation: 1497

You need to extend ProviderObserver and override didUpdateProvider

ProviderObserver listens to the changes of a ProviderContainer.

Here is the official explanation

Upvotes: 1

Related Questions