I'm newbie in this flutter. I have watched some videos about getting data from API and I see some of them creating a model for the data got from API. But I don't know why they need to create it because without the model, the code is still working right .

Can somebody explain this for me @@ ?


Solution 1: Saiful Islam

A Model represents a custom data object. It helps to work efficiently with custom data. Suppose you have Data of user information and you get it from API response and it looks like something bellow


user: {
    name: 'user name',
    age: '25',
}

You get a response something like this HashMap representation or JSON data. Now you can access all those user information like...


map['user']['name'] // this line gives you the name of user
map['user']['age']. // this line gives you the age of user

there is no problem with that kind of use of JSON data

But in Object-Oriented Programming its good to represent all your Custom data as a real-world Object (if possible) suppose you have the user information as a HashMap so it's better to convert your HashMap to actual real-world Object then it's going to give you a wonderful experience while you using your data through your application.