如何监听 Riverpod 中 ProviderContainer 的变化?

How to listen to changes on ProviderContainer in Riverpod?

在纯 Dart 项目的上下文中,我想“观察”依赖项的变化。如何收听变化?

至于read()我想我一切都很好。

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

您需要扩展 ProviderObserver 并覆盖 didUpdateProvider

ProviderObserver listens to the changes of a ProviderContainer.

Here is the official explanation