02 Adding Model to the API call | Flutter REST API Series

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello there in this video i am going to continue the previous video so in the previous video we were like writing code like this user um square bracket name first picture thumbnail so these code can be improved there are few problem with this code which i will show you in a moment so first of all i will create a folder called models and i will create a file user dot dot so i will create a class called user for user model so this class will consist all the property which are object present in this object so we are having gender which is strings of gender a constructor named constructor with required gender okay that's it so let's pick the next property email so i like i skip some of the property because those are complex like name is complex location is complete i will pick this at the end of the video so let's do the simpler one first so let's do the phone cell is similar so let's avoid it let's use nationality cell okay let's use cell also okay okay so there are some complex one name location we will do at the end of the video so do not worry everything looks good we are having user okay so earlier we were having list of dynamic now we will have list of user let's import it and this is done okay so now it is giving me some errors so we need to fix it so how we can do user dot and you see these are the possible value which are available in user so now we don't need to like put the square bracket anymore and write the key we can just use like dot and from the model it already knows what are the value which are available there okay now if i refresh the code i will face the problem and the reason will be this one so now what i need to do is that i will take the json result okay and yeah so these are the results so now i cannot like put the results directly in the user because this is not of type list of users so you see um this is like type of list of dynamic so i will write as list of dynamic because the type is not defined so now i will map it so map will basically take each object one by one and i will transform it into something else okay so i can simplify this a little bit more so i can create a variable called transformed and put it in this here and i can assign the transformed here awesome so now what i will do i will like uh take the result map them into the user now i have to provide the cell number so e is the like the single user object so let's update it to the user or let it be both are fine so i write cell which is good later we have to pass the email this one is also good gender e gender moving nationality e nat nationality and phone e-phone okay that's it so things look good if i rephrase i should get the data yep i am getting the data okay moving further if i update it to the gender you see male female i am getting it so i can make or get the value very easily so now i am going to change the color or set the color of tile based on the gender so let's set up define a variable called color and use it or gender if that will be male let's set a color colors dot blue and colors dot red you see so based on the gender we are setting it so it's awesome it's it's simple awesome and cool you can do lots of thing basically i am saying you showing you uh you are having the value now we don't have to write the key we just have to write dot and it will give me all the possible values so we have lots of things here so instead of email like not instead uh along with email let's say something else user dot email and user dot phone okay so now we have two information user or email and phone so things look good so this one is transforming so this part is little bit important where we are transforming the thing now we should move further and make one of the complex yeah like mapping a complex uh object so inside the user we have user name so username is having title and [Music] first name and last name first and last okay so we have three property and username and this is going to be required first and let's paste it title and last okay so now this one looks good now i will create this one with name name i missed the semicolon okay awesome yeah now i'm getting one error here so i have to pass name so i cannot do something like this e name this will be wrong and this was the main reason i didn't like do the name at very beginning so how can we do this one so let's uh use a variable because this is going to be complex so here i will write name and here i have to tell first name e user and first title and title and last and last so this is how we can do the name so you see the name should be of type username okay things look good here let's click on this it should work no response i'm doing something wrong let's open the console it is complaining about this line okay so the title was called on now okay this should be name user of name of title user of name of first okay so now this should work fetch user called fetch user completed okay so let's uh make this as name so name dot first you see we are getting let's remove the color part it is looking bad yeah now you see we are having this one if you want to make last we can do it in the next video we will move further and improve this code more and make it more production ready and readable
Info
Channel: Nitish Kumar Singh
Views: 9,097
Rating: undefined out of 5
Keywords: Nitish, Kumar, Singh
Id: -48PDXzVefI
Channel Id: undefined
Length: 8min 36sec (516 seconds)
Published: Tue Aug 16 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.