Flutter hooks for StateNotifier
StateNotifier Hook
This package provides a useStateNotifier
hook similar to useValueListenable
. There is also
useCreateStateNotifier
hook which creates a StateNotifier
and automatically dispose it.
Usage
// 1. Create your state notifier as usual.
class CounterNotifier extends StateNotifier<int> {
CounterNotifier() : super(0);
void increment() => state++;
void decrement() => state--;
}
// 2. Extend hook widget
class ReadMeExample extends HookWidget {
const ReadMeExample({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
// 3. Create your notifier (useCreateStateNotifier will dispose it)
final notifier = useCreateStateNotifier(() => CounterNotifier());
// 4. Listen to your state
final state = useStateNotifier(notifier);
//......
}
}