onManualInvalidation method
- @experimental
- void cb()
A life-cycle for whenever this provider is manually invalidated, as opposed to automatic invalidation caused by dependency changes.
This callback is triggered when:
- invalidateSelf is called
- invalidate is called on this provider
- refresh on this provider
This callback is NOT triggered when:
- A dependency watched with watch changes
Common Use Cases
Invalidation forwarding: Forward manual invalidations to upstream providers:
final sourceProvider = Provider((ref) => 'source data');
final derivedProvider = Provider((ref) {
final data = ref.watch(sourceProvider);
// Forward manual invalidations to the source
ref.onManualInvalidation(() {
ref.invalidate(sourceProvider);
});
return 'processed: $data';
});
Returns a function which can be called to remove the listener.
See also:
- invalidateSelf, to manually invalidate this provider
- invalidate, to manually invalidate other providers
- refresh, to forcefully re-evaluate a provider
Implementation
@experimental
RemoveListener onManualInvalidation(void Function() cb) {
_throwIfInvalidUsage();
final list = _onManualInvalidationListeners ??= [];
list.add(cb);
return () => list.remove(cb);
}