#Google's Flutter Tutorial-Generate JSON Models,Filtering ListViews,Delay Searching-coderzheaven.com

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome back to another flutter tutorial in this video I'll be showing how to auto generate these and models in product so let's start so for this we need some libraries the jacent model will run a decency realizable and this is an annotation so three of them are dev dependencies since we don't need those in the list bills okay so let me show the adjacent that we are going to create the model so you can get this from this in placeholder dot happy car calm okay so here you can see a list of users we have ten user accounts so we're going to create a model based on this so let me copy one user now go to the project root folder and create a folder named J sounds so this is a default folder but you can have your own name as well in that case you need to specify that name when you run the command in the terminal okay so place it there the uses dot user dot JSON file and let me open the terminal and we need to run a command to generate a model okay let me expand that and run flutter packages Pub run these are not as good model so if you are running this command for the first time you may get some error then you just need to read on it okay now it's done so go back to the project folder and go to the Lib folder there you should see a new folder models so you can see that the user model has been generated so you have the necessary functions as well okay now let's see the other file so this is a generated code we should not modify this code okay so let me close that and when you go to the user dot JSON file you can see that there are some nested these and optics the address okay so we need to generate a model code address as well so if you go to the user dot dot file so you can see that it's a string and a dynamic because it doesn't know what is inside so let's create a model for address so I am going to the Jason shoulder and create address dot Jason and go to the user dot JSON file and cut the object address and paste it in at the store Jason ok save it now the value will be dollar address so that's a file name okay so let's the read out the command ok it's done now let's go to the Immortals folder and you can see that artist model has been generated now if you go to index you should see two boats and so you can see that there is another nested object inside address that's a Geo so it's a map string dynamic so if you go to the user tour dot you can see that the address has been imported now okay so now let's create a model for geo as well okay so we need to go to the ultra store JSON file and cut the G object create a new file geo dot decent and place it there okay and the video will be dollar deal ok run the command you know it is done now let's go to the models folder and you can see that the Geo model is created now if you go to address you can see that the geo is imported now if you go to use it or Jason you can see that there is one more mystery object company let's create that company door Jason so cut and paste object here now go to company door Jason and paste it there and change the variable to dollar company run the command again and wait for it to finish ok done now let's check the models folder so you can see that company model has been generated and if you go to the user tab you can see both address and company if you click on it you can see the corresponding model ok now we know that it's a list of users right so you can see we have 10 records here so it's a list of uses so we need to create a model for that as well for that I'm creating a new file I'm going to name it users dot JSON ok and an object users and the value will be the list of users right so dollar array and you sir so that's file name so we have a list of users here right now let's run the command ok successful go to the models folder so you can see the uses tour dot file so the member variable is the rest of yourselves so there is our user model so we have successfully generator models for our or Jason objects in that service now we are going to write his service to get the data so we need the HTTP package so I will be providing the link in the description for all plugins okay create a new file services dot dot there we will write the service set import the necessary packages we need the convert package HTTP package and model classes you sent out dot and you sister dot right now class services let's declare our URL so static string URL is equal to so that's our URL okay now be able to try to method to get a users from this URL so get users is going to return a future users and if there is some error you're going to turn the error and we are going to wrote down an empty set of users you can do whatever you want here and throw some error or okay so get the response so final response is equal to http dot get URL and okay so list of users as you go to power stored map so we are going to look through the list of Jason and create the user and finally convert that well list create a new users object and I say in the users and return it okay now let's go back to the main file and import the data sync services and the models so we need the users ok we need a variable to hold the users and another variable title now let's hover right in state method utilize the title to loading users and users to new users and call services target users not then let's name it reduces from server and assign the value to users and called said state okay let's update the title as well so let's say I do it the door title so that's the title now let's create a hairless widget to show the list of users so we did list and return expanded and we are going to add the ListView as a child of expanded so let's see you taught builder item count will be the length of users so that's a list of users and item builder will have the build context and the index we need to return a list row here so let's write a new function row we'll Tetro and pass in the index so we're going to return a card child with a padding of 10 and a codon budget which will have three children the first one is a text this will display the name in black Allah and is spacing and third one is a text which will display the email and the color will be gray okay now let's add the list of the UI okay there we have our list now we will had a search text field on top of the list okay so we'd said search text field that's going to return a text field and let's give some decoration so input decoration and border outline input border and border radius so circular water radius so radius of five okay let's set the property filled to true and give a fill color so I'm going to give a white sixty and a content padding or 15 in text let's say filter by name or email okay let's add the hauntings property of the wrecks field so that will have the parameter a string so whatever the user types and that is filled to the UI give some spacing okay run the app now we have our text field now let's write some functions and classes to start searching on the list when the user types something on the textview so we'll be starting the search when the user actually stops after typing something in the text field okay so let's write a new class T bouncer which will accept milliseconds and a callback and a timer so the constructor will accept milliseconds and a run method that's going to have the action as parameter and if the timer is not not we are going to cancel it so we'll be canceling the timer if the user types within that time frame and we will create a new timer with the duration milliseconds and the second parameter is the callback so that will be action okay now let's create a new variable finally bouncer is equal to nu D bouncer let's pass in five thousand that's five seconds so when the user stops for five seconds we'll start searching okay now let's go to the on chase with load of the text field and call D bomb set or run and the callback in the callback will be calling the service again and searching in the result okay so things are tight a little searching and call the service and get the data again okay now we need to search in this response so we'll be writing a new function I'm going to write the function to filter the users in the users model itself you can write it anywhere you want so static users filter list and that's going to accept users and the filter string create a temporary users and asking the users to it now we are going to loop through the users and find the matching users okay so list of users is equal to temp you silh start users don't wear and we're going to look through it and we need to search for name and email right so user dot name to lowercase what contains filter filter string and we had to do the same thing for email as well copy and paste it again since add to email now we have the filtered users so assign the uses property to the new users and root on it okay go back to the main file called the function filter list and update the users variable so you say start with the list users from server and string okay and change the title to the previous one okay restart the app let's try filtering so when I stop it will wait for five seconds and then it will start searching okay so it's working but the title is not updating to searching so let's fix that I think let's see in the timing - one second and the title of its width it tatta it will change that to title here restart the app okay now it will start searching after one second when the user stops typing let's try something else okay well win okay soaking perfect in this way we are not searching on every keystroke of the user so we are putting less button horn with the device and server so that's an efficient way of searching okay so that's all in this video hope everyone understood how to automatically generate a set models in clutter creating a filter on a list and do a reader searching if my video is helpful please give me a thumbs up and share it hit the bell again for notifications when I upload new videos thanks for watching see you in the next video until then bye
Info
Channel: Mobile Programmer
Views: 14,154
Rating: undefined out of 5
Keywords: flutter tutorial, flutter examples, flutter filtering, flutter json, flutter code, flutter google, flutter dart, flutter listview, flutter listviews, google dart, coderzheaven, the mobile programmer, coderzheaven.com, mobile programming, program flutter, flutter service calls, flutter listview filtering, flutter coderzheaven, more tutorials, learn flutter, cross platform, Json models, generate json models, json serialize, json annotation
Id: YUOjcER6DFE
Channel Id: undefined
Length: 19min 32sec (1172 seconds)
Published: Sun Jul 28 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.