onUnmounted static method

void onUnmounted(
  1. void callback()
)

Registers a callback to be called when the widget is unmounted.

The callback is called when the widget is removed from the widget tree.

Parameters:

  • callback: The function to call when the widget is unmounted

Example:

setup: (context) {
  final timer = useSignal<Timer?>(null);

  onMounted(() {
    timer.value = Timer.periodic(Duration(seconds: 1), (_) {});
  });

  onUnmounted(() {
    timer.value?.cancel();
  });

  return (context) => Text('Timer');
}

Implementation

@pragma('vm:prefer-inline')
@pragma('wasm:prefer-inline')
@pragma('dart2js:prefer-inline')
static void onUnmounted(void Function() callback) {
  final currentContext = useSetupContext();

  currentContext._onUnmountedCallbacks.add(callback);
}