onCancel method

  1. @override
void onCancel(
  1. void cb()
)
override

Add a listener to perform an operation when the last listener of the provider is removed.

This typically means that the provider will be paused (or disposed if using Provider.autoDispose) unless a new listener is added.

When the callback is invoked, there is no guarantee that the provider will get paused/dispose. It is possible that after the last listener is removed, a new listener is immediately added.

See also:

  • keepAlive, which can be combined with onCancel for advanced manipulation on when the provider should get disposed.
  • Provider.autoDispose, a modifier which tell a provider that it should destroy its state when no longer listened to.
  • onDispose, a life-cycle for when a provider is disposed.
  • onResume, a life-cycle for when the provider is listened to again.

Implementation

@override
void onCancel(void Function() cb) {
  _onCancelListeners ??= [];
  _onCancelListeners!.add(cb);
}