What is Data Model in flutter?
When dealing with data that comes from API or database, this data represents something, maybe an employee's data on ERP app, products data on a shopping app, This data is the backbone of your app, this is the data that makes your app make sense.your app is consist of ( UI + Data), you should know first what is the shape of your data, what you are going to show on your app, and that is before start coding UI.
For example:
I will make an app that shows my employees in a company,
First, I will create an Employee class that describes my employee info ( name, age, salary).
Second, When I will create a list of (objects of Employee class):
Second, When I will create a list of (objects of Employee class):
List<Employee> employees = [
Employee(name:Mark,age:29,salary:1000),
Employee(name:Jon,age:25,salary:1200),
];
Then, I will make a custom widget that shows Name, Age, and Salary in ListTile, so I can use them on my app interface as a list of the custom widgets, And custom widget needs data to show it, so I will pass the first object in the list.
employees[0].name
employees[0].age
employees[0].salary
And put this data in the first custom widget in my ListView() for example.
State Management in Flutter using Provider | for beginners
Why use "const" in your flutter code?
Employee(name:Mark,age:29,salary:1000),
Employee(name:Jon,age:25,salary:1200),
];
Then, I will make a custom widget that shows Name, Age, and Salary in ListTile, so I can use them on my app interface as a list of the custom widgets, And custom widget needs data to show it, so I will pass the first object in the list.
employees[0].name
employees[0].age
employees[0].salary
And put this data in the first custom widget in my ListView() for example.
State Management in Flutter using Provider | for beginners
Why use "const" in your flutter code?
Summary
Data Model is a custom class that you use to convert the list of data that came from API or database to an object that you can use on your widget tree, you can get a list of (strings or int) and use it with no issue, But you can’t get a list of (employees) because it is not identifiable from IDE or Fluter.
So you define it to your code and tell it that there is a data type or model called (class name), and its features are (class variables and attributes), so IDE can understand what is and employees look like, and use this model like you use any data type (String, Double, or int ....etc)
The data model is very important in big apps, like Facebook you will create a lot of data models, and you have to learn how to manage this data model in your app through StateManagement techniques Provider is the recommended way from google and the Flutter team itself.
You can see how to use state management in flutter using Provider | for beginners
⭐⭐⭐ Buy Food for my Dog 🐶 ⭐⭐⭐
https://buymeacoffee.com/ramy.dmk