onEffectCleanup function

void onEffectCleanup(
  1. void cleanup(), [
  2. dynamic failSilently = false
])

Registers a cleanup function for the current active effect.

cleanup is the function to be called when the effect is cleaned up. failSilently determines whether to suppress warnings when called outside an effect.

final count = ref(0);

effect(() {
   print('Count: ${count.value}');

   // Triggered when the effect of count changes.
   onEffectCleanup(() => print('Cleaned up'));
});

Implementation

void onEffectCleanup(void Function() cleanup, [failSilently = false]) {
  if (activeSub is private.Effect) {
    (activeSub as private.Effect).cleanup = cleanup;
  } else if (dev && !failSilently) {
    warn(
      'onEffectCleanup() was called when there was no active effect to associate with.',
    );
  }
}