03 Api Call Code Refactoring | 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 made a model in this video i am going to call the api without clicking on the floating action button so we will remove this floating action button so now the api will be called without clicking off button so how it will happen so in any stateful visit there is a init state so whenever that widget get rendered init state get called so in the init state we will call fetch users okay so this is a fetch user so the return type should be future async so now i am going to improve this code little bit more so currently if we go to the model you will find that we created two models in one file so now i am going to create another file for user name so now we are having username here so now i need to import this here so the model is now also improved the button is improved i'm getting error so i need to import the model here okay so now things look good here but like this should not present here like this logic should not be in the ui file so for this we will create one folder called services and inside this services i will say user api dot dot awesome so now i will create a class called user api and i will paste what is present in previous files so let's copy it and paste it so i will get few errors so we need to solve it so let's comment this one so you see that these these things are not being used in this file so this must be related to the fetch user thing okay so now it is complaining that switch user is not defined let's comment it okay so everything looks good it is complaining about the model so we need to import this model here and it is complaining about the set state because set state cannot be called outside of the state full widget so this one looks good everything is good let's remove the print statement also so now the fetch user will be called in different file so how we will get the value in different file so for that what we can do we can return list of user because this is the use of fetch user method like to return the list of user so let's return transformed and so instead of void i will write list of user so now the transform is being returned so let's update the variable name of transform to the users okay things look good now so now i will use this user api class and make this method as static so let's uncomment this a fetch user so let's write void fetch users and let's write user api and let's import the user api now and dot fetch user so you see that just by writing this line the api call is being made so you see that there is future which means it will take some time to return the response so we need to write await and make this method as a sync so now we got the users or we can say response because response is like same as user so now i will say users equal to response so that's it so you see how much simplified this file looks like now it is returning list of users so earlier we were having this much of code so now all that is equivalent to this one still those code like is available in our project but it is in different files so you see it is still working and it's it's good so now let's improve this more so you see that we have name here okay we have name so but like i want to get the full name so for that i will make a string get full name so this is known as getter so in the getter what i will do i will i will concatenate the name so how i will do it so i will say this dot named this or i can directly say name dot title okay and this is how i can get the title this is how i can get the title after that i will get the first name and i can get the last name that's it so now first name is a getter so let's come here so instead of name dot last which was very big i can directly write full name i will get the full name so this is also how you can improve the code and increase the readability of the code so this was mainly about the how you can improve the code and write bitter code so that other developer can come and easily look into your code without getting confused so i hope that this makes sense and this is more like production ready thank you
Info
Channel: Nitish Kumar Singh
Views: 14,233
Rating: undefined out of 5
Keywords: Nitish, Kumar, Singh
Id: 7xh9PmQtj-k
Channel Id: undefined
Length: 5min 39sec (339 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.