onManualInvalidation method

  1. @experimental
RemoveListener onManualInvalidation(
  1. 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:

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:

Implementation

@experimental
RemoveListener onManualInvalidation(void Function() cb) {
  _throwIfInvalidUsage();

  final list = _onManualInvalidationListeners ??= [];
  list.add(cb);

  return () => list.remove(cb);
}