river_pod
a simple way to access state while robust and testable for flutter
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
this project can be considered as a rewrite of provider to make improvements
that would be otherwise impossible.
long story short:
- 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()); } }
motivation
if provider is a simplification of inheritedwidgets, then riverpod is
a reimplementation of inheritedwidgets from scratch.
it is very similar to provider in principle, but also has major differences
as an attempt to fix the common problems that provider face.
riverpod has multiple goals. first, it inherits the goals of provider:
- being able to safely create, observe and dispose states without having to worry about
losing the state on widget rebuild. - making our objects visible in flutter’s devtool by default.
- testable and composable
- improve the readability of inheritedwidgets when we have multiple of them
(which would naturally lead to a deeply nested widget tree). - make apps more scalable with a unidirectional data-flow.
from there, riverpod goes a few steps beyond:
- reading objects is now compile-safe. no more runtime exception.
- it makes the provider pattern more flexible, which allows supporting commonly
requested features like:- being able to have multiple providers of the same type.
- disposing the state of a provider when it is no longer used.
- have computed states
- making a provider private.
- simplifies complex object graphs. it is easier to depend on asynchronous state.
- makes the pattern independent from flutter
these are achieved by no longer using inheritedwidgets. instead, riverpod
implements its own mechanism that works in a similar fashion.
Comments are closed.