Skip to main content ppwriters

State management

Setup riverpod for managing state in our application

In this lesson, we will learn how to setup riverpod for state management in our application.

What is state?

State is the data that is used to render the UI of an application

  • State can change over time
  • State can changed based on user interaction or other factors such as network response.
  • State can be local to a widget or shared across the application

What is state management?

State management is a technique to manage the state of an application for effectively updating the UI whenever the change occurs.

  • The simplest way to manage state is the StatefulWidget which is used to manage state locally in a widget.
  • InheritedWidget which is used to share state across the widget tree.
  • We can use these simple state management techniques for small applications.
  • For complex state shared across multiple screens, we need state management libraries
  • State management libraries provide a way to share state across the application and update the UI whenever the state changes.
  • They help manage the state in a predictable way and make it easier to maintain and make changes to the application.

Setting up riverpod

To setup riverpod, we need to add the following dependency to our pubspec.yaml file:

dependencies:
  flutter_riverpod: ^2.4.9

We can also do the same from the terminal.

flutter pub add flutter_riverpod

Finally, run the following command to install the dependency:

flutter pub get

Next, we need to import the flutter_riverpod package in our main.dart file.

import 'package:flutter_riverpod/flutter_riverpod.dart';

And, we also need to wrap our root widget with ProviderScope widget.

void main() {
  initDependencies();
  runApp(
    ProviderScope(
      child: MainApp(),
    ),
  );
}

With this we should be able to use riverpod for sharing state across our application. Which we will do in the next lesson.

You are currently learning Flutter & Appwrite. An ultimate course to build cross-platform applications using Flutter - the cross-platform application development framework by Google and Appwrite - the open source backend as a service.

Curriculum