A state-management library that:
-
catches programming errors at compile time rather than at runtime
-
removes nesting for listening/combining objects
-
ensures that the code is testable
Declare your providers as global variables:
final counterProvider = StateNotifierProvider((ref) {
return Counter();
});
class Counter extends StateNotifier<int> {
Counter(): super(0);
void increment() => state++;
}
Use them inside your widgets in a compile time safe way. No runtime exceptions!
class Example extends ConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
final count = ref.watch(counterProvider);
return Text(count.toString());
}
}