useUnmount function

void useUnmount(
  1. VoidCallback fn
)

Flutter lifecycle hook that executes a function when the component is unmounted.

This hook runs the provided function when the component is about to be unmounted (destroyed). It's useful for cleanup logic such as canceling timers, closing streams, or removing listeners.

fn is the function to execute on unmount. The function reference is updated on each build, so the latest version will always be called.

Example:

useUnmount(() {
  print('Component unmounting!');
  // Cleanup: cancel timers, close streams, remove listeners
  timer?.cancel();
  subscription?.cancel();
});

Note: If you need both mount and unmount functions, use useLifecycles instead.

See also:

Implementation

void useUnmount(VoidCallback fn) {
  final fnRef = useRef(fn);

  // update the ref each build so if it change the newest callback will be invoked
  fnRef.value = fn;

  return useEffectOnce(() => () => fnRef.value());
}