Flutter Class Models Demystified || How to create Models in Flutter

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back to another video in this video we'll learn uh how to create class models in flutter so I have a project setup already so in main.dot the basic template so I'm calling the home widget which is inside this screens folder so I've just created one app bar and I've used the list Builder to uh display text 10 times okay so this was so this is the basic code template I already have this is the output so what I'm going to do is I'm going to create a list of data let's say student data to display it in our flutter lab so I'll just create a list I'll name it as student s and each student should have a name so I'll write the name then let's say uh uh his roll number which is empty jump then he has his blood group right okay then let's say we have S marks okay uh let's say anything so so to keep it simple we'll just use this four data so obviously there are going to be multiple students right so we'll just cut this Ctrl X I create another list inside that student list which is which we called nested list so in this I'll just uh paste it so I'll create another list let's say this is and his roll number is one one and this blood group is positive and he has server score of 18. so I'll just copy paste this multiple times and in the name I'll just add one number so that we can see a difference okay and I'll so we have created one student list and I'm just going to print that list in the list Builder I will just give the length of student and in our text using string uh so we'll be we'll be displayed displaying the student data so from IE index we'll have to first print name then we have to print first index which is roll number where we have to print second index which is split group then we have to print third index which is marks so we'll just try to hot restart it and we'll see how all the difference so we we got the output in our app so all it all the data in this list has been displayed so this method is quite simple it's straightforward it's simple we are able to do it but in larger projects let's say we want to add more data to this or we want to reorder this data so it will become difficult for us to identify uh which type of value is stored in which index now we know that name is stored in first index what if it was reordered in some way and we we can't find the index of the name of blood group or stuff like that so here is where uh model class comes in so we'll convert this list into model I will show you how so we'll create a folder called models and inside this we'll add all our models models of our project so in this case we just have one we have to create one model which is stored and model so it's basically a glass and we'll Define all the values that we have to use which we have used in this list so first is name right second is okay let's say blood group right uh next is roll number next is marks so now what we'll do is we'll create a Constructor for this so we'll create a Constructor and generally Constructors are like you create a parenthesis and outside that we give our curly braces right in this case we will close this class and inside the parameters will give our curly braces okay and we'll have to uh pass our Global variables which we have given here so how will pass the global variables how will reference the global variables using this keyword so I'll just add this and I'll put a dot on it so we get a list of all the variables which we have uh defined it in our class so first we have name then we have black group then we have marks then we have roll number so this is how you create a model class so but you can see that we have an error here so to solve this so basically there are two ways we can solve this one is we can Define that this name can be null so we'll just put a question mark next to the data type so we are telling the class model that this variable this ah name can be of null type or if it can't be null type then we have to add a required statement here which means that if you pass this class model which is student model then mandatory will have to pass this uh name right if we don't pass this we can pass a question mark here we can give a question marker which will say that this is optional the values can contain string or it can be empty which is null so in our case ILS I'll just add required to all the values so this is how we create class models so now we will see how to replace our list to this class model so what I'll do is I'll just delete all this but I'll keep the list now I'll Define the list uh I'll give a I'll Define this list I'll tell this list what what all the values it will contain so it will contain student model so it's very generic right so inside this student list I'll pass instead of nested list earlier which we used here we'll use student model if you just type student model if you have the uh intelligence automatically it will display you all the parameters here but what I will do is I'll just delete this and I'll show you from scratch so what we did we created our we wrote our class name with the parenthesis okay now inside this to find what all parameters we can pass we'll just click on control space okay uh so we have four values which here which we can pass so first value is name so you can pass our name here second is let group third is marks for this row number right so we created one model of for one store and similarly we'll do it for the students I'll just copy paste this below okay now we have defined our list inside this list we have used we have passed our class model which is student model so now we have to use this value instead of passing indexes so it makes the process very simpler and uh and easy to uh get the values out of it so what we'll do is we will delete this I'll write from scratch so we have to pass student which is the list name and inside the student will pass the index 0 0 index first index okay and let's say when index is 0 it will call it will be reference to this class model so from this class model we have to access name blood group marks and stuff all the parameters all the values so I'll just add one dot and we can see a list of options that we can see and I'll just click on name so instead of using this index I am directly saying that from this index from the list pass name so this becomes way more simpler and we'll pass other values as well so we have roll number we have blood group and we have marks so I'll just save it and I'll give a hot restart we can see that we got all our values so the only difference is earlier we use index which can get complex when we have larger projects um and we have replaced that with class models so class models can be used in many ways is one of the uh where that we use uh it will be mostly used in API calls and stuff where you have multiple uh parameters so and we can convert that into class models we'll see that in the next video until then show your support by subscribing to this channel see you in the next video
Info
Channel: Techness With Hussain
Views: 2,733
Rating: undefined out of 5
Keywords:
Id: 7dRIY4hT9nU
Channel Id: undefined
Length: 11min 17sec (677 seconds)
Published: Sun Jun 18 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.