Mastering Freezed in Flutter under 20 minutes | Flutter favourite #flutter #freezed

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today we are going to talk about the freeze package so it is the flutter favorite package why it is so first of all let's learn what is freezed so its core generation for immutable classes that has a single Centex API without compromising on the features so this means that if we have the API if we have to perform some copy with method or like here you will see that Dart is awesome but defining a model can be tedious so we have to define the Constructor over I2 string operator double equal to Hash code functions copy with from Json to Json functions so it becomes very difficult to write all these things with our hand so it takes a lot of time and we have to implement the the real features the logic side of things is not to build this complex model classes so ah you have already learned about the quick type or Json to dot models but most of the time they are not successfully generated the correct result so we are using and we are learning how the flitter favorite package work for us so for the installation purpose we have to go to how to use and here uh we just need to copy these things okay so let's copy freeze denotation build Runner and the freezed package itself so now we have to go to the terminal and here I'm going to paste all these lines okay so ah you will see that in the pubspec.yaml file there are something like yeah in the dev dependencies build Runner and the freeze have been added freeze annotation is also there okay so uh Builder will build the model class for us freezed and freezed rotation will do to generate the freezed things for us okay now ah we have to go to the like let's get some example and how we can do this so ah let's do one example okay we have ah let's make a class here and in the class let's create a new file and let's make it more bigger so that you can see it so if I just write here percent.dart and here let me write one person class so it's a class name as a person and then extends I don't need to write the extent for now we need to create one Constructor for it and the Constructor is a type of string for example name okay and then the other could be the age okay so for example age is required for us so it means that we can make the optional uh variables and required variables too with the freeze now uh here we have to make this class as a freeze so let's write here the freeze okay ah freezed ok so you will see that it just imposed the freezed annotation package for us so we can we have to use this freezed annotation of this class now uh we have to write here equal to sign and then uh then we have to write the person again that's it okay after this this function should be the factory Constructor this Constructor should be the factory Constructor okay perfect now here we have to write the mixins so we have to write here the dollar sign underscore dollar sign and then person I hope so we are not doing wrong things now perfect now uh we have to make uh right we have to write a class name as a a class name should be the same name of the person okay so person dot dot freezed dot dot our package our freezed to copy hash code and all necessary uh generator functions will be generated in this class okay so that's why I just write here the part now we have to go to the [Music] um we have to copy this command and let's go to the terminal and let's write here this command paste it and press the enter so it's denoting something and you will see that person dot freeze dot dot has been generated that's it but you will understand it will generate the copy with method which will have help us for the caching uh the other methods to that is like the person with implements tostring method operator double equal to methods so these are all the functions and all the things that he can generate for us uh otherwise we have to write all these lines for us for with our hand so it becomes very difficult and a tedious task and a time taking task two now the main thing how to connect a from Json function and two Json function okay so for this ah let's create uh I have the postman and in the postman you will see that I have one API that I just get from the internet and this have all these data so count entries apis description and all these things okay so basically there are two objects mainly in the response of this API so you can ah sorry you can get it and you can practice it if you need it I'm just showing you in a zoom function and I will just send you in the link too okay so now how can we use it in our flutter project so ah firstly we have to uh what was the name of it let's check it so let's write here the public okay so public in the public we have two things countries and entries okay so let's create a one other folder named as a model public model or whatever you were right public model okay now let's create a new file there and I will just write here public dot art let's create a new class here and I will just write here the public so in the public you will consider that the count and the entries so I'm just going to write here first of all create a Constructor and in the Constructor just write here string count okay and after this just right here underscore click okay this function should be the factory so just make it Factory also just right here the width and right here uh underscore dollar sign public perfect now I think so everything is good we have to make it uh make this class as a freezed annotation just right here the freezed after this we need to make a part and it should be a part of public dot dot okay so the file name and this part should be the same so propart dot freezed dot dot now we have to go here and we have to run the same command again filter pub run buildriner build so uh you will see that it just it will build the same thing for us again uh where it is uh it should it is saying that there could be some error public dot freeze dot dot must include the part directive um in the input Library okay warning freeze on lib slash public model dot dot okay so let me check why it is showing oh uh we have to write here the public not the freeze so this is my mistake um now let's run it again and uh yeah you will see that this function have been generated but we have to make the from Json and 2 Json function here so for this simply we have to create a factory Constructor again here and just need to write here factory and the name is the public Dot from Json and it should be the type of map string and the type should be the object and the name and after this we have to write underscore dollar sign the name should be the public from F should be the capital and then Json J should be the capital and then here just right here the Json okay perfect so you will see that it has been it looks like this thing uh our dysfunction has not been completed yet so what I will do I will just uh run the same command again but before running this we have to do one more thing okay and that one more thing is uh firstly we have to uh we have to uh go there and if using freeze to generate from Json and 2 Json we have to add both these dependencies so let us copy it and now let's go here and paste it again okay so both have been added after adding this one we have to write here the part and we have to write the public dot g dot dot so public dot g dot dot will separately generate the from Json and two Json method for us okay let's click on the terminal again and let's write let's use the same dependent same command again in the terminal so it will generate the from Json and two Json function for us uh it's running still we have to wait for it so you will see that public dot g dot dot this function have been generated so when we just write the from Json it will automatically uh get the two Json function for us if we just use it so let's write here public Dot from Json so you will see that so after calling the API we can call this function now uh uh we have the API which have something like entries to which is a list of some maps Okay so now let's create ah uh the increase here okay so in the model we can write here a new file and new file could be right here the increase dot dot before this let's go to the public dot dot and here the first value is the count which is a type of string okay integer sorry so we can write here the integer so after this you will see that we have the entries which is a type of list okay so we can write here list of type entries okay entries here I can write here the entries again till Now entries is not defined so what we will do we have to create the entries dot Dart file and here we have to write the class entries after this we have to create the entries and we have to write here the string what values it should accept the first one is the auth HTTP course and Link okay so for example I'm just going to copy the HTTP so HTTP is a type of Bool so I can write here the Bool and I can write here this HTTP after this we have the description which is a type of string so I can write here a string and this string is like this description okay so it should be the the nullable value or it should be the required value then we have to write the entries entries okay perfect uh we have to make it a const and then a factory okay now we should write the width and then dollar sign in trees okay the name should be the same here too and here 2. but with underscore here one thing to remember uh is we have to use the freezed package freeze annotation here we have to make a call the entries the name should be the same the file name should be the same so entries Dot freezed dot dot also we have to write the part entries dot g dot dot for creating two method from Json and two Json function so from Json or to Json function firstly we have to write here um you know we had to write here uh we have to write here the factory Constructor and then we have to write here entries Dot from Json and here we have to put here the map string object and then we have to write here the Json whatever the value you you will write and after this underscore dollar sign entries F should be capital and GA should be capital and here this variable should be called again okay so you will see the overview of it we have just created this function we have to create now model class for it just remember that this name it is good this name should be like the entry not the entries okay so it should be the entry because class name should be the singular not the plural so entry and also the entry okay so all the name should be the same so entry entry entry entries here should be the entry and here entry and this should be remain same it doesn't matter let's run this command again and let's see what we can see okay can we see something here uh yeah entries dot freeze dot dot and entries dot cheetah dot have been build and that's it now let's go here and let's write here the entry okay and uh when we are creating the entry now we have to run the same command again you will see that oh there is some issue the parameter entries of public is not nullable okay so it says that you have to put the question mark or you have to put here the required keyword it's completely up to us so let's put here the question mark and now let's run this command again perfect so in the public dot g dot you will see that entries and count both have been completed and in this way uh you can use the copy with 2 Json at all these functions should be generated and should be work accordingly okay so thank you for uh watching me please subscribe this Channel and press the Bell icon
Info
Channel: Just Flutter
Views: 1,743
Rating: undefined out of 5
Keywords: FlutterJSONSerialization, FlutterFreezedPackage, JSONParsingInFlutter, FlutterTips, FlutterTricks, flutter code generator ai, flutter code generator, freezed package flutter, freezed flutter, freezed tutorial in flutter, what is freezed package, flutter tutorial, flutter tutorial for beginners, flutter, flutter advanced
Id: Pat_7Yevx2A
Channel Id: undefined
Length: 19min 21sec (1161 seconds)
Published: Sun Jul 09 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.