onScopeDispose function

void onScopeDispose(
  1. Disposer fn, {
  2. EffectScope? owner,
})

Registers a cleanup function to be executed when the current effect scope is disposed.

Parameters:

  • fn: The cleanup function to register
  • owner: Optional effect scope owner. If not provided, automatically detects the current active effect scope from the reactive context.

This function can only be called within an effect scope context. The cleanup function will be executed when the effect scope is disposed.

Example:

final scope = EffectScope()
  ..run(() {
    final subscription = someStream.listen((data) {});
    onScopeDispose(() => subscription.cancel());
  });

Implementation

@pragma("vm:prefer-inline")
@pragma("wasm:prefer-inline")
@pragma("dart2js:prefer-inline")
void onScopeDispose(Disposer fn, {EffectScope? owner}) {
  assert(owner != null || getActiveScope() != null,
      "onScopeDispose can only be used within an effect scope");

  ((owner ?? getActiveScope()!) as EffectCleanupMixin).onCleanUp(fn);
}