Member-only story
Experiments with the new Riverpod 2.0 Notifier
Learn some details about using Riverpod 2.0
Introduction
Version 2.0 of Riverpod was released on Sep 30, 2022. At the moment it has reached version 2.1.1. So it is quite fresh and there isn’t yet much info available on medium.com or Youtube. Even the documentation on riverpod.dev is not yet updated with details.
The best information I found on Andrea Bizzotto’s blog at: https://codewithandrea.com/articles/flutter-riverpod-async-notifier/
Riverpod 2.0 adds two new provider types together with their modifiers autoDispose and family:
- NotifierProvider
- AsyncNotifierProvider
The documentation doesn’t mention them yet, but maybe you will find them when you are reading this article later than November 2022. In this article, I will only use NotifierProvider. Maybe in a second part, I add an experiment for AsyncNotifierProvider.
To learn more about NotifierProvider I created a little example app. It is a bit similar to the standard counter app but uses Riverpod to increment and display several counter values. I added print statements at the interesting spots and configured an observer to monitor all changes in Riverpod’s container.
The Source of the Providers
Here is the source of the CounterNew class, a minimalistic counter:
This class extends the new Notifier and uses the corresponding NotifierProvider to make it globally available. The build() method reacts to changes of the dependyProvider and updates the _delta value accordingly. This _delta is added to the state when the increment method is called. The print statements will show when one of these interesting spots is executed: