Search⌘ K

The provider Package

Explore how to implement state management in Flutter apps with the provider package. Learn to separate UI and logic by using ChangeNotifier and ChangeNotifierProvider to update and consume app state reactively. Understand how to install the provider package and integrate it into your app to keep the UI synchronized with the underlying data changes.

Introduction to the provider package

The provider package uses InheritedWidget under the hood. It offers a simplified, low-boilerplate approach to state management. The provider package makes it easy to separate the UI and logic of an application.

Defining provider at the top of a widget tree makes the app state accessible to all child widgets.

The provider added to the child in the widget tree
The provider added to the child in the widget tree

The provider state can then be accessed down the widget tree

In this tutorial, we’ll update the state of the app below using provider:

Bud1�lsdsclboolmodelsdsclboolpresentationdsclbool @� @� @� @E�DSDB ` @� @� @
State management with the provider package—starter code

Using the provider package

We first need to install the provider package. Locate # TODO-1: Add provider package dependency (line 17) in the pubspec.yaml file and install the package, as shown below:

YAML
provider: ^6.0.2

The provider package exposes different objects that our application can use in state management. In this lesson, we’ll work with the ChangeNotifierProvider object. We’ll need to understand the following concepts:

  • ChangeNotifier: This is a class that provides a change notification API.
...