Slide 1

Slide 1 text

Flutter with Riverpod and Code Structure Tanmay Soni MACS @Concordia University

Slide 2

Slide 2 text

Tanmay Soni MACS @Concordia University | Flutter Developer @Common ● 4yr+ Software Developer (Flutter, React.js, Node.js, Java …) ● https://www.linkedin.com/in/soni-tanmay/

Slide 3

Slide 3 text

Agenda 1 2 3 4 5 6 Riverpod Introduction & Installation Riverpod Implementation Code Demo Code Structure Packages & Tools Q&A

Slide 4

Slide 4 text

Riverpod Introduction & Installation

Slide 5

Slide 5 text

Why Riverpod? RiverPod => A Reactive Caching and Data-binding Framework ● Declarative and Reactive Programming. ● Handles a significant portion of your app's logic. ● Built-in Error Handling and Caching. ● Automatic Re-fetching

Slide 6

Slide 6 text

How to Install ● flutter pub add flutter_riverpod ● flutter pub add riverpod_annotation ● flutter pub add dev:riverpod_generator ● flutter pub add dev:build_runner ● flutter pub add dev:custom_lint ● flutter pub add dev:riverpod_lint Alternatively, you can manually add the dependency to your application’s pubspec.yaml file

Slide 7

Slide 7 text

Code Generation flutter pub run build_runner watch We need to run this to enable auto code generation. With this, every time we modify the riverpod-related code, it automatically generates it.

Slide 8

Slide 8 text

Enabling riverpod_lint/custom_lint ● Riverpod comes with an optional riverpod_lint package that provides lint rules to help you write better code, and provide custom refactoring options. ● The package should already be installed if you followed the previous steps, but a separate step is necessary to enable it. To enable riverpod_lint, you need add an analysis_options.yaml placed next to your pubspec.yaml and include the following:

Slide 9

Slide 9 text

Riverpod Implementation

Slide 10

Slide 10 text

Before we start making network requests, make sure that ProviderScope is added at the root of the application. Setting up ProviderScope

Slide 11

Slide 11 text

● All providers must be annotated with @riverpod or @Riverpod(). ● The name of the annotated function determines how the provider will be interacted with. For a given function myFunction, a generated myFunctionProvider variable will be generated. ● Must specify a "ref" as first parameter. Creating the provider (Functional Provider)

Slide 12

Slide 12 text

Example

Slide 13

Slide 13 text

● All notifiers must override the build method. This method is equivalent to the place where you would normally put your logic in a non-notifier provider. ● When a @riverpod annotation is placed on a class, that class is called a "Notifier". The class must extend _$NotifierName, where NotifierName is class name. ● Notifiers are responsible for exposing ways to modify the state of the provider. ● Public methods on this class are accessible to consumers using ref.read(yourProvider.notifier).yourMethod(). Defining a Notifier (Class Provider)

Slide 14

Slide 14 text

Example

Slide 15

Slide 15 text

● A Consumer is a widget similar to Builder, but with the added benefit of offering us a "ref". ● This enables our UI to read providers. ● Widgets can listen to as many providers as they want. To do so, simply add more ref.watch calls. Consumer

Slide 16

Slide 16 text

● Instead of writing a StatelessWidget/StatefulWidget returns a Consumer, we can define a ConsumerWidget/ConsumerStatefulWidget. ConsumerWidget / ConsumerStatefulWidget

Slide 17

Slide 17 text

ref.watch(): Any widget that is listening to this provider will rebuild whenever the provider’s state changes. ref.listen(): Go-to for reacting to state changes with side effects, such as showing a SnackBar or logging. ref.read(): Typically used for accessing a provider’s current state without listening to it. Whenever possible, prefer using ref.watch over ref.listen or ref.read to implement a feature. This way, the application becomes reactive and declarative. But it is recommended to use ref.read when logic is performed in event handlers such as "onPressed". + + + How to read provider state?

Slide 18

Slide 18 text

Code Demo https://github.com/soni-tanmay/Flutter_Riverpod_Clean_Architecture

Slide 19

Slide 19 text

Code Structure

Slide 20

Slide 20 text

Folder Structure

Slide 21

Slide 21 text

Packages & Tools

Slide 22

Slide 22 text

Packages ● flutter_screenutil ● go_router ● flutter_gen ● build_runner ● flutter_localizations ● freezed ● icons_launcher ● riverpod_generator ● sqflite ● dio

Slide 23

Slide 23 text

Questions?

Slide 24

Slide 24 text

THANK YOU! v