This project modifies the default starter app (in main.dart
when creating a new flutter app) to use the Hooks Riverpod state management package.
Inspired by Surgach.
Hooks Riverpod is 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
environment: sdk: ">=2.16.2 <3.0.0"
dependencies:
cupertino_icons: ^1.0.2
hooks_riverpod: ^1.0.3
Models generally handle the logic of an app. In this case, it is the state management class, and it represents the state of the counter. The model is called CounterModel
and is used to display the current valye of the counter in the middle of the screen.
The text which changes on the button tap is refactored into a separate widget, CounterTextWidget
to avoid rebuilding the entire MyHomePage
widget.
The state management class is called CounterNotifier
. it extends the StateNotifier
class.
A StateNotifier
has a single internal variable, state
that holds the current state, an instance of [CounterModel].
An increment()
method is also added to the state management class so an outside class can call increment
to replace state with a new instance of CounterModel
, whose internal count
value is one larger than the last instance of the CounterModel
.
In the main.dart
file, wrap MyApp
with a new widget ProviderScope
.
Create a global provider using a StateNotifierProvider
because the state
is in a tateNotifier
class, CounterNotifier
.
CounterTextWidget
extends HookConsumerWidget
to use the ref
commands provided by Riverpod.
ref.watch
listens for state changes in the provider which is the current instance ofCounterModel
. This also provides a reference to the notifier state, stored in theCounterModel
.- Now when the state changes, the
build
method will re-run to update the new state.
The Text widget can easily display the new state by calling counterModel.count
as this contains the integer value of the state.
Similarly, the value of the Text widget can be updated by calling the increment
method using ref.watch
.
Tapping the + button only rebuilds the CounterTextWidget
as seen from the debug console (thanks to the print statements).
For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.