manageChangeNotifier<T extends ChangeNotifier> function
Manages a ChangeNotifier with automatic lifecycle management.
Similar to manageListenable, but also handles disposal of the ChangeNotifier when the component unmounts.
Use this for controllers and other ChangeNotifier instances that you create and need to dispose.
Example:
@override
Widget Function(BuildContext) setup() {
final controller = ScrollController();
final reactiveController = manageChangeNotifier(controller);
final scrollOffset = computed(() {
reactiveController.value; // Track changes
return controller.offset;
});
return (context) => ListView(
controller: controller,
children: [...],
);
}
Implementation
ReadonlyRef<T> manageChangeNotifier<T extends ChangeNotifier>(T notifier) {
final custom = manageListenable(notifier);
// manageListenable already detaches its listener on unmount (registered
// first, so it runs before this dispose in FIFO order).
onUnmounted(notifier.dispose);
return custom;
}