Why state management is important ?
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.
Benefits of State-Management:
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 .
Then ,I should use state management in every app ,That's right ?
we can't say that.
if : the data of app is little and the app is small => you shouldn't use state management .
what is provider ?
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.
Let's create app and Learn How to implement Provider on default counter app ?
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 " master
"
read this article for more details :
Part 1
Dev : https://lnkd.in/dHZJs5sC
Medium : https://lnkd.in/dgeqiM86
Part 2
Dev : https://lnkd.in/eVZx82Av
Medium : https://lnkd.in/evtQVtws
provider_state_management
A new Flutter project.
Getting Started
This project is a starting point for a Flutter application.
A few resources to get you started if this is your first Flutter project:
For help getting started with Flutter development, view the online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.
Congratulations 🥳🤩
you implemented Provider in Default Flutter Counter App !
run:
Ok , what is difference between ValueNotifier , ChangeNotifier and StateNotifier ?
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 " Value_Notifier
"
read this article for more details :
Part 1
Dev : https://lnkd.in/dHZJs5sC
Medium : https://lnkd.in/dgeqiM86
Part 2
Dev : https://lnkd.in/eVZx82Av
Medium : https://lnkd.in/evtQVtws
provider_state_management
A new Flutter project.
Getting Started
This project is a starting point for a Flutter application.
A few resources to get you started if this is your first Flutter project:
For help getting started with Flutter development, view the online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.
Top comments (0)