Provider State Management in Flutter - Part 1
Tags: flutter, provider, statemanagement, dart
Published: 2022-07-24 16:41:00

 <u>**_Why state management is important ?_**</u>  
Application state management is the process of maintaining knowledge of an application's inputs across multiple related data flows that form a complete business transaction or a session to understand the condition of the app at any given moment. In computer science, an input is information put into the program by the user and state refers to the condition of an application according to its stored inputs saved as variables or constants. State can also be described as the collection of preserved information that forms a complete session .So, without some form of state management. the performance of the application will reduce.

**_<u>Benefits of State-Management:</u>_**
**1-** The State of the Whole Application is present at a single place, so we do not need to access the single state or data from different places.
**2-**It reduces the HTTP requests sent to the back-end for fetching and retrieval of the data.
**3-** helps to centralize and made the maintenance of code very easy, also it improves the quality of code, by reducing the code size and making it more readable .
**<u>
_<u>Then ,I should use state management in every app ,That's right ?</u>_</u>**
we can't say that.
 if : the data of app is little and the app is small => you shouldn't use state management .

 **_<u>what is provider ?</u>_**
The provider package is easy to use which is basically a wrapper around the Inherited Widgets that makes it easier to use and manage. It provides a state management technique that is used for managing a piece of data around the app.

**_<u> Let's create app and Learn How to implement Provider on default counter app ?</u>_**

`flutter create provider_state_management`

**add the dependency for the provider pattern in the pubspec.yaml file.**
`flutter pub add provider`

```
dependencies:
  flutter:
    sdk: flutter
  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^1.0.2
  provider: ^6.0.3
```

**create your model and add a method to increment the counter, extend `ChangeNotifier` from the material.dart package. This provides us with the `notifyListeners()` method, and will notify all the listeners whenever we change a value.**

```
import 'package:flutter/material.dart';

class Counter with ChangeNotifier {
  int counter = 0;
  // int get_Counter() => counter;

  void incrementCounter() {
    counter++;
    notifyListeners();
  }
}
```

**wrap your home / materialApp widget with `ChangeNotifierProvider<YOUR_MODEL_NAME>` widget**

```
@override
  Widget build(BuildContext context) {
    return  ChangeNotifierProvider<Counter>(
       create: (context) => Counter(),
      child: MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home:MyHomePage(),
    ),
    );
  }
```
**To update the text, call your variables and function from Your model to use them
wrap your text widget with `Consumer<YOUR_MODEL_NAME>` widget**

```
  Consumer<Counter>(
              builder:(context, count, child) => Text(
                '${count.counter}',
                style: Theme.of(context).textTheme.headline4,
              )
            ),
```
**Source Code**: branch  " <u>`master`</u> "
{% embed https://github.com/eman55555/provider_state_management %}
                  
_**Congratulations 🥳🤩**_
_**you implemented Provider in Default Flutter Counter App !**_
**run:**  {% embed https://images.app.goo.gl/jh6ywrvR1P28tkFE9 %}

**_<u>Ok , what is difference between ValueNotifier , ChangeNotifier  and StateNotifier ?</u>_**
`ValueNotifier `is a special type of class that extends `Changenotifier`, which can hold a single value and notifies the widgets which are listening to it whenever its holding value gets change.
`ChangeNotifier `is a class that provides change notification to its listeners. That means you can subscribe to a class that is extended or mixed in with `ChangeNotifier `and call its `notifyListeners()` method when there’s a change in that class. This call will notify the widgets that are subscribed to this class to rebuild.
On the other hand, `StateNotifier` is an immutable state management solution where the state can be directly changed within the notifier only.

**So, We can implement `ValueNotifier `on the same counter app**

**Source Code**: branch  " <u>`Value_Notifier`</u> "
{% embed https://github.com/eman55555/provider_state_management %}