낭초비 2024. 7. 3. 11:53
반응형

 

When are providers initialized?

All Riverpod providers are lazy-loaded. (링크)

  • The provider's state is initailized when the first listener is registered
  • Every time the state changes, all listeners will be notified so they can update/rebuild themselves

Both ref.watch() and ref.listen() can be used to register as a listener to a provider. This is in contrast with ref.read(), which only does a one-time read and does not register a listener.

 

When and how do they get disposed?

If we declare a provider without autoDispose, its state will remain alive until the enclosing ProviderScope is disposed (unless we explicitly dispose it in some other way)

If we declare a provider with autoDispose, its state will be disposed as soon as the last listener is removed(typically when the widget  is unmounted) 

Providers are auto-disposed by default if we use the @riverpod syntax, meaning that we don't keep the state once it's no longer needed

 

Provider Lifecycle Callbacks?

  • ref.onDispose: triggered right before the porvider is desroyed
  • ref.onCancel: triggered when the last listener of the provider is removed
  • ref.onResume: triggered when a provider is listened again after it was paused
  • ref.onAddListener: triggered whenever a new listener is added to the provider
  • ref.onRemoveListener: triggered whenever a newlistener is removed from the provider
반응형