What is Sate Mangement ?
What if you are going to build a shopping app, and you need to manage products data from multiple screens, I mean we need to use and access product data throughout the whole application, this is what state managements exist for, it let you able to use the data from anywhere in the app, and not that only !!
The most important thing is that when you change the data from anywhere in the app, it should change on the whole app, and this simply happens when we use Provider, which is one of the state management methods, and it's the one recommended by the Flutter team
The most important thing is that when you change the data from anywhere in the app, it should change on the whole app, and this simply happens when we use Provider, which is one of the state management methods, and it's the one recommended by the Flutter team
How to use Provider?
Provider in simple explanation, you can imagine it like (data holder) or let's say it is just like a table in the database, we store employee's data on a table, and this table has fixed fields (name, age, salary) and we add a row with every employee, and when we need to get Mark data, we search first for the table of employees and then get the data, and use it somewhere else.
let’s say Mark got promoted and his salary increased so (data change) happened now, so it must update the info in the table, so when the HR department calls the data of this employee, they will get the right new data, not the old one.
(ads1)
let’s say Mark got promoted and his salary increased so (data change) happened now, so it must update the info in the table, so when the HR department calls the data of this employee, they will get the right new data, not the old one.
This is a simple way to explain to you what is Provider in Flutter
1- you will create a class with its fields and methods (Table)2- and will you will call an object from this class ( single row of the table) ex. mark
3- you will use the data of this object (row) anywhere on your flutter app widget tree.
Example:
class Employees extend ChangeNotifier{
String? name;
int? age;
int? salary;
}
You can create many objects of this class, Employees1(), Employees2(), ...etc, and use them on the app, but you will not be able to control them from anywhere.
But when you use Provider,
1- you will create and use only one object of this class throughout your whole app.
2- you will add it at the top of your widget tree, and use this object to( get) or (set) the data stored on this object’s variables.
in this example, we use it to store and manage one employee, but if you need to use it to manage more than one employee, we will use a LIST of employee objects and this is called the Data Model. Do know what is a Data Model?
in this example, we use it to store and manage one employee, but if you need to use it to manage more than one employee, we will use a LIST of employee objects and this is called the Data Model. Do know what is a Data Model?
Now you will have a new data type, i.e new class called Employee, and you will have another class called Employees, which is our data of employees provider, and it contains:
1- a list of (employee objects)
2- setter and getter methods, to get and update data on the list
3- and any extra methods you need in this Provider.
Summery
1- a list of (employee objects)
2- setter and getter methods, to get and update data on the list
3- and any extra methods you need in this Provider.
Summery
You can consider the Provider as Smart DataBase, containing (data = variables) and methods to control this data (select - update - delete) or methods to convert the data that came from API to represented data in my class.
⭐⭐⭐ Buy Food for my Dog 🐶 ⭐⭐⭐
https://buymeacoffee.com/ramy.dmk