Provider Setup

Tutorial 3 of 4

Provider Setup Tutorial

1. Introduction

In this tutorial, we'll walk you through the Provider setup. The Provider package is a popular state management solution in Flutter. It allows us to manage the state of our application by providing data to widgets down the tree.

Goals:

By the end of this tutorial, you will learn how to set up and use the Provider package in a Flutter application.

Prerequisites:

  • Basic knowledge of Dart and Flutter
  • Flutter SDK installed on your machine
  • A text editor (like VS Code)

2. Step-by-Step Guide

First, we need to add the Provider package to our pubspec.yaml file:

dependencies:
  flutter:
    sdk: flutter
  provider: ^5.0.0

After adding the package, run flutter packages get in the terminal to fetch the package.

Provider Types

The Provider package comes with different types of providers for different use cases:
- Provider: The most basic form of provider. It takes a value and exposes it.
- ChangeNotifierProvider: Listens to a ChangeNotifier, exposes it to its children, and disposes of it when needed.
- FutureProvider: Takes a Future and updates dependents when the future completes.
- StreamProvider: Listens to a Stream and exposes the latest value emitted.

Provider Usage

To use a provider, we need to wrap our widget tree with the provider and pass the class instance we want to provide to the down tree.

3. Code Examples

Let's look at a ChangeNotifierProvider example. We first create a Counter class that extends ChangeNotifier:

class Counter with ChangeNotifier {
  int value = 0;

  void increment() {
    value += 1;
    notifyListeners();
  }
}

In this class, whenever increment is called, we increment the value by 1 and then call notifyListeners() to notify any widgets listening to this change.

Next, we wrap our widget tree with ChangeNotifierProvider:

void main() {
  runApp(
    ChangeNotifierProvider(
      create: (context) => Counter(),
      child: MyApp(),
    ),
  );
}

Now, we can access the Counter instance anywhere below MyApp in the widget tree:

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final counter = Provider.of<Counter>(context);

    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text('Provider Example')),
        body: Center(child: Text('Value: ${counter.value}')),
        floatingActionButton: FloatingActionButton(
          onPressed: counter.increment,
          child: Icon(Icons.add),
        ),
      ),
    );
  }
}

In this example, when the FloatingActionButton is pressed, counter.increment is called, which increments the value in the Counter instance and calls notifyListeners(). This causes the widgets that are listening to this Counter instance to rebuild, updating the displayed value.

4. Summary

In this tutorial, we learned how to set up and use the Provider package in a Flutter application. This package allows us to manage the state of our application and provide data to widgets down the tree. We also learned about different types of providers and how to use them.

For further learning, try using other types of providers, like FutureProvider and StreamProvider.

Additional Resources:
- Provider Package on pub.dev
- Flutter documentation

5. Practice Exercises

  1. Create a simple to-do list application using the ChangeNotifierProvider. The app should allow adding and removing to-do items.

  2. Modify the Counter App to decrement the counter as well. Add a second FloatingActionButton that, when pressed, decreases the counter.

  3. Try using the FutureProvider by fetching data from an API and displaying it.

Remember, the best way to learn is by doing. Happy coding!