avatarsimbu

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2024

Abstract

e0">Getting started</h2><figure id="eda5"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*w0ceGYTUEGYxwdscl5lmPg.png"><figcaption>Add to pubspec.yaml</figcaption></figure><figure id="3a07"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*v2bdAfOL-voeGwgsREW6mQ.png"><figcaption>Import package in main.dart</figcaption></figure><figure id="995e"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*X_j8J_tikmNK-SI4EesMjA.png"><figcaption>Add ProviderScope to main.dart</figcaption></figure><p id="6ee7">You can then declare providers where ever you want. I opted to add them to separate .dart files and then include them where needed.</p><figure id="eaaf"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*vkTtUCfy9JKRL6VGfxGOuQ.png"><figcaption>Providers in seperate files</figcaption></figure><p id="ea85">To consume providers you need access to the Riverpod’s state container through it’s ‘ref’ property, you do this by changing the widgets type:</p><figure id="7ff4"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*pOgFEThxoknN_xPMzg5hAA.png"><figcaption>Riverpod widget types with access to state</figcaption></figure><h2 id="4a07">Simple read only provider</h2><figure id="f0eb"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*Jd3a8U5SMT2NKahwttDOqA.png"><figcaption>Declare read only provider</figcaption></figure><p id="39c4">In stateless widgets ‘ConsumerWidget’ you need to add WidgetRef ref to the build method.</p><figure id="e18e"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*eL5ojYlws8VFIlJrgUYSxg.png"><figcaption>Consume in a stateless widget</figcaption></figure><p id="ca3a">In stateful widgets ‘ConsumerStatefulWidget’ ref is available everywhere and does not need to be declared on the build method.</p><figure id="f5be"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*_HBImNCwlGUKc8rVP__7jg.png"><figcaption>Consume in a stateful widget</figcaption></figure><h2 id

Options

="3687">Future provider for API calls</h2><p id="43c8">The examples shows the direct use of the API client, when I add authentication it will move out into its own provider.</p><figure id="4ce5"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*EUxoO_aFTBWQW7lVT7cXmg.png"><figcaption>Declare future provider for an API request</figcaption></figure><figure id="599a"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*AwyXivpm0EgwtPyzjRedvw.png"><figcaption>Consumer future provider in stateless widget</figcaption></figure><p id="cea8">I like the ability to just use the .whenData extension on .watch to implement the widget when the data is available and to provide a default .value during loading and failure. This may change if I need to add animations or skeletons during loading.</p><p id="73fb">It is important to use .watch for all values in build methods where you wish the widget to rebuild when state changes, equally not to use it inside callbacks like onTap, use .read instead.</p><h2 id="8faa">XP</h2><h2 id="368d">ProviderScope Error: ‘!_dirty’ is not true</h2><figure id="9f02"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*wD2tR7fnPh_Ml6X9rUJIpA.png"><figcaption>ProviderScope Error</figcaption></figure><p id="dfa3">Invalid syntax that was auto formatted to this by vscode</p><figure id="0b6b"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*DhvBP24KvRKnh0wKnBTcLQ.png"><figcaption>Code that caused the error</figcaption></figure><p id="0217">But needs to be this</p><figure id="f6d5"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*8sfbzqDjycBEulGyzD8hIw.png"><figcaption>Correct code</figcaption></figure><h2 id="0055">Links</h2><ul><li><a href="https://riverpod.dev">Riverpod</a></li><li><a href="https://readmedium.com/flutter-hooks-7754df814995">Flutter Hooks</a></li><li><a href="Flutter-Firebase(Firestore)%20CRUD%20App%20using%20Riverpod.">CRUD Riverpod & Flutter with Firebase</a></li></ul></article></body>

Join Medium to view all my articles.

Flutter — Riverpod

Subscribe to view all the articles, or check out the code.

We can now build new features and widgets through tests, it is now time to harden what we have and get ready for production and working with a larger team.

For a long time I’ve held off changing away from simple state management using ‘Provider’.

But now I’m looking to be able to access state without a build context, avoid runtime ProviderNotFoundExceptions, improve its testability, tidy up the ChangeNotifier code that has no real pattern, move towards immutable state where needed and have better support for Authentication and async API’s calls across different environments.

I reviewed the current big three in state management, Bloc, GetIt, Riverpod and choose Riverpod, try them, it really comes down to personal choice and project / team size.

Riverpod is still a locator pattern where you use a ref to the container and as such requires that your code is coupled.

For me the key advantage was it is simple to create small bits of state rather than large nested objects and then combine them where needed.

I going to start with some basic examples to retrieve recipes and provide some application configuration state.

My intention is to update this blog further when experiences and usage patterns emerge with the project, like authentication, filtering and sorting…

Ta Da

Getting started

Add to pubspec.yaml
Import package in main.dart
Add ProviderScope to main.dart

You can then declare providers where ever you want. I opted to add them to separate .dart files and then include them where needed.

Providers in seperate files

To consume providers you need access to the Riverpod’s state container through it’s ‘ref’ property, you do this by changing the widgets type:

Riverpod widget types with access to state

Simple read only provider

Declare read only provider

In stateless widgets ‘ConsumerWidget’ you need to add WidgetRef ref to the build method.

Consume in a stateless widget

In stateful widgets ‘ConsumerStatefulWidget’ ref is available everywhere and does not need to be declared on the build method.

Consume in a stateful widget

Future provider for API calls

The examples shows the direct use of the API client, when I add authentication it will move out into its own provider.

Declare future provider for an API request
Consumer future provider in stateless widget

I like the ability to just use the .whenData extension on .watch to implement the widget when the data is available and to provide a default .value during loading and failure. This may change if I need to add animations or skeletons during loading.

It is important to use .watch for all values in build methods where you wish the widget to rebuild when state changes, equally not to use it inside callbacks like onTap, use .read instead.

XP

ProviderScope Error: ‘!_dirty’ is not true

ProviderScope Error

Invalid syntax that was auto formatted to this by vscode

Code that caused the error

But needs to be this

Correct code

Links

Flutter
Mobile
Programming
Dependency Injection
iOS
Recommended from ReadMedium