Slide 1

Slide 1 text

Effective State management using Riverpod Gaurav Bhatnagar Satish Kumar Mobile Team

Slide 2

Slide 2 text

Why “State Management” ? • Managing state is something most developers come to deal with when working on flutter applications • When do you know that you need state management? • It is not necessary that every widget needs its own state management some can be just manage with stateful widgets and setState.

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

What is a Provider ?

Slide 5

Slide 5 text

Let’s say for instance we have a Tugboats class that handles numberofTugboats and another class Vessel which dependson the Tugboats class.

Slide 6

Slide 6 text

If we were to have both as providers, then combining them will look like this.

Slide 7

Slide 7 text

Runtime Exception

Slide 8

Slide 8 text

It is difficult to have more than one provider of the same type, the one closest to the widget intended will always be accessed and hence causing unintended access.

Slide 9

Slide 9 text

Riverpod • Riverpodis dart solution built from scratch to overcome the complexity of Provider • A Provider(Riverpod) is an object that encapsulates a piece of state and allows listening to that state • Wrapping a piece of state in a provider – ✓Easily accessing that state in multiple locations. ✓Enables performance optimizations. ✓Increases the testability of your application. ✓Allows easy integration with advanced features, such as logging or pull-to- refresh.

Slide 10

Slide 10 text

State management in Action - Change Notifier • Theming(Dark/Light) the Mobile application • Global provider to hold the state • .watch, .read, .listen, .autoDispose

Slide 11

Slide 11 text

Future and Stream Provider • FutureProvider is typically used for – • performing and caching asynchronous operations (such as network requests) • nicely handling error/loading states of asynchronous operations • removes redundancies of FutureBuilder • StreamProvider is like FutureProvider but for Stream instead of Future. • StreamProvider is usually used for – • it allows other providers to listen to the stream using ref.watch. • it removes the need for having to differentiate broadcast streams vs normal streams. Creating the configuration would be done with your typical async/await syntax, but inside the provider. Using Flutter's asset system, this would be:

Slide 12

Slide 12 text

ProviderReference

Slide 13

Slide 13 text

Modifier : Family If you want to add external information to construct your provider (that is not necessarily another provider)

Slide 14

Slide 14 text

State management in Action – StateProvider • State provider change the state using StateNotifier • Global provider to have state of list items added to cart • Watch the provider state in the app(wherever it needed) • Call to action to change the state

Slide 15

Slide 15 text

Testability • Since providers are usually declared as global variables, you might worry about that one. After all, global state makes testing very difficult, because it can require lengthy setUp/tearDown. • But the reality is: While providers are declared as global, the state of a provider is not global.

Slide 16

Slide 16 text

Conclusion • Riverpod helps manage state of application in a safe way with flexibility of having global scope, similar type of providers and reference-based access. • It is dart-based solution(independent of flutter) where state can be easily autoDisposed when it is no longer used. • Improves Readability, Scalability , Testability and simplifies complex object graphs • If you have an existing app using provider, it is relatively easier to migrate.

Slide 17

Slide 17 text

References : • https://bit.ly/3ycyxmy - Flutter State Management with Riverpod: The Essential Guide • https://bit.ly/3waAXPM - Riverpod: A deep dive "on the surface" by Kefeh Collins • https://bit.ly/38WkOW9 - The State of Flutter by tomerpacific • https://bit.ly/3LPxsFj - Fun with Flutter Video Blog Series • https://riverpod.dev/ - Remi Rousselet • https://github.com/rrousselGit/riverpod

Slide 18

Slide 18 text

No content