Build Your Own Custom Code Generator in Flutter | Flutter Code Generation

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
have you ever heard of packages like freezed retrofit Json serializable and Riverport generator what do they have in common they're all code generators meaning they generate code from code in this video we are going to learn to build code generator from scratch we are going to build a code generation tool such that we don't have to write methods like to map from map copy with for every single class we create in our flutter project we just have to write it once for our code generation tool run a command from terminal and see the magic happening live let's get into it the first step is to create and set up a code generation project so go in your desired folder where you want to create your project create an empty directory and let's call this code generation then migrate to that folder and open attend vs code why we created an empty directory the reason for that is we need three subdirectories one is the annotations folder second is the example flutter project where we are going to test out our code generation tool and the third is the logic for our code generation and the actual code generator so here I'm going to press command shift p from my keyboard run the flutter new project command and first I'm going to generate a flutter normal application I'm going to select this directory so I'm going to go ahead here select the folder and name this example after this is done exit this folder again open it in vs code we have our example flutter project now we need two more projects so again press command shift p flat a new project but this time we're going to generate a package not an application so click over here we're going to create this in the code generation folder itself and this will be called annotations we'll discuss more about the subfolders in detail in just a minute but let's just create them and set it up first again we'll close vs code and open it a third time then we can again press command shift p flut a new project and again a new package we'll select the code generation project again and we are going to call this generators we'll again close this open attend vs code again and we have all the three projects showing up here so what are these three folders first one is the annotations folder as the name suggests it will help us to create an annotation what are annotations let's go to the main.dot file here you can see at the rate override this is an annotation if you've used immutable classes there's either it immutable that is an annotation basically if we suffix a keyword with add the rate and it exists in flutter or dot SDK that is an annotation so we are going to create an annotation for our class we'll discuss more about this when we get there next is the example flutter project so we are going to test out our annotations here and the third one is a generator so we are going to have the actual logic of code generation right over here so let's get started with The Code by going to the annotations folder first go to the lib folder and go to the annotations dot dot here we have a simple Library created we'll just remove this calc calculator class already created of course the test gives us an error so we can remove everything from the test as well or delete the entire test here and save it if you are concerned about the warnings you can remove that as well we're not going to cover the testing part over here and in the lip folder here we are going to create another folder called SRC source so that all the logic part goes into one folder and let's call this Json generator dot dot this is not a generator a code generator this is just for annotation so if you want to name this Json generator annotation that would be a good thing but since it is already in the annotations folder let's just call it Json generator here we are going to create a simple class called Json generator annotation and we are going to create a const Constructor for this it's important to create a Constructor because only if the class has a constant Constructor can we create an annotation based on this and outside of this class we are going to create a variable called Json generator which will be equal to Json generator annotation save it and we have created The annotation that's how simple it was we just create a class we have a constant Constructor and we create a variable for this now anytime we want to annotate something we can just call add the rate Json generator like this and it will work now we can come to the annotations.file here we are going to call Export SRC slash Json generator dot dot so from this annotations Library we are just exporting one file which is Json generator.dot if you want to create more annotations for different purposes for your code generation tool you can create all of them in the SRC add them to the annotations and Export them so whenever you use this annotations Library somewhere else which we are going to it will just import one file which is annotations.dot it doesn't have to import every single file now we can close this annotations is now complete now let's get to the generators part we are going to create a new file here called build.yaml and after that we are going to configure something here but before moving there we can go to the pubspect.dml file so this is our normal pubspect.aml file but I want to add some dependencies and Dev dependencies so that I can do my code generation work much easily so the first dependency we have here is build this build package lets us inspect classes and give access to different elements of class like you have variables methods classes Constructors of those classes this build method or this build package is going to give everything I'm not specifying the version number because I always want it to be the latest version from pub.dev next is the source generation package this Source generation package or Source gen is a wrapper around this build package some of the methods that build gives us might not be friendly for us to use so Source gen is a wrapper around this so that we can use it quite easily basically it gives us friendly developer apis next is the annotations package and this is not a package that is available on pub.dev it is this package that we want to import why are we importing it we'll get to know when we understand how code generator will work after setting up this I'll make sure you understand why we are importing this and how code generation overall works but for now we are going to import this annotations package so we just have to type your path because this is not from pub.dev it's from our local system only so the path is dot dot slash so we are right now in this generators folder and this is our pub spec.tamil file so we need to get out of this folder so we'll do dot dot slash and then we need to go to annotations folder so I can just type annotations now done but now this is giving us an error because publishable package cannot have path dependency basically if you are publishing this generator this code generator is being published to pub.dev it should not have anything that's present locally right so we have to make sure that this is not publishable on pop.dev so we can just type here publish to none if we have this the error goes away because it can't be published to pub.dev now so these are all the dependencies we need now let's go to the dev dependencies so we need build Runner so build Runner will allow the generation of files from the code that we write here and if you want to test your application you can add test and build test packages but it's not for us so I'm not installing them and these are all the things we need now we need to configure the build.aml file so if you go to this link it will be mentioned in the description below and copy all of this paste it here you'll get access to the build.aml configuration these are available all over the Internet so you can find them but I'll explain what all of this is we'll come back to this after we write the code generation logic here now let's get to the left folder in the generators dot dot and first try to understand how the code generation works so suppose in our example file we have a class called person so we are going to create person.dot we have a class we'll call this person and it has some properties so it will have string name String last name Boolean is adult and let's just say int age if we import our code generation tool and just add suppose at the rate some annotation name after we do that and run a command which is this we'll understand this when we actually generate code but for now this is the command it will generate a new file for US called person.g dot dot so this was exactly what we had in build.tml file what should the output of the generated file be so it will be person.g dot dot and it will contain the logic that we write in our lip folder for code generation what we want to achieve is creating a Constructor based on all of this a two map function a from map function and a copy with function so how does our code generation Tool Work Well the beauty is in the add the rate annotation part so what our code generation will do is just search wherever in the folder our annotation is used after we get to know that we are going to visit every field element present here we are going to visit the Constructor element so that we get the class Name by field name I mean it will visit all of these fields get their variable name get their data type and based on that we will create our code generation tool pretty simple so we are going to save this out for now let the error be there in the lib folder again create a source folder create a file called Model visitor dot dot and this function will be used to visit all the elements of a class once we find The annotation so we are going to create a class called Model visitor and we're going to extend this with something known as simple element visitor and now you can see we are getting this error we are getting this error because you have not imported the class from which the simple element visitor comes and that is we have to import it and it comes from the analyzer package so we have analyzer dot slash element slash element dot dot we still don't have it so we have to import another line called from the same folder but this will be called visitor.dot and now we have this element is for some other file which you'll get to know in just a second but this comes from the visitor.file now you might wonder where did this analyzer package come from so if you go to pubspect.aml file you've imported search generator and built if not import anything like analyzer it's there because build and Source generation both use analyzer within them so it gives access to the analyzer package as well because these packages have been have exported this analyzer package here we also need to mention the generic type for this class so it's going to be simple element visitor with a void now whatever functions we override from the superclass are going to have this return data type which is void if you type in here all of the functions that we are going to override are going to be having the return data type as int now let's see what methods we need it's not giving us any auto suggestion so we'll just go in this class and see what functions it has to offer there's visit augmentation import element we don't know what that means visit class element that sounds familiar we do want to know the class name visit compilation unit element visit Constructor element this will be more useful visit an enum element with an extension element visit field element so we want to know all the properties of the class so like the data type as I mentioned the variable name this will help us with that visit function element there's no need as of now because we don't have any functions or we don't even care if there are any all we care about are fields and classes so we are going to have these two visit Constructor and visit field so let's override them so we are going to have at the rate override this is also an annotation by the way we are going to have a return data type of void visit Constructor element and here it's giving us an error if we come back here you can see it also has a parameter here we can copy this paste it here and now we have that this is what the element.dot gives us the Constructor element the field element all of those elements if we remove this import you can see this gives us an error if you want to remove this warnings you can just click on ignore depend on reference packages for this file and the warnings go away now let's write the logic for Wizard Constructor element basically the simple element visitor will visit all of the elements inside of a class inside of a file so we are visiting the constructed element with the help of this function now we just need to get the class name that's of purpose so here I'm going to create a global variable called class name we're going to store whatever class name we get inside of this variable and now I'm going to get the class name so I'm just going to have final return type equal to element dot return type this Constructor element has a bunch of properties but element dot return type dot tostring will give us the class name so we are visiting the Constructor element right so what is the return type of that Constructor Constructors always have the return the type of a class right so it's going to return a class to us but right now if you try to print it and run this of course it won't work but trust me on this one basically if you just do return type it will give you something like orson's star for this kind of class it's not giving you just person it's giving you person star I'm not sure about that reason why it does that but I can suggest a fix for this so I can just have class name equal to return type dot replace first so we're going to replace the star that we get first with an empty string so if there's person star you are going to replace this Asterix with an empty string so it will just go away as simple as that and this is going to be our class name now we want to visit the field elements because we want to get access to string name String last name all of that so let's go to a simple element visitor let's see what other field we add visit field element so you're going to copy that I have your wizard field element since this was a constructed element this had Constructor element since this is a field element this is going to have field element here I'm going to create a map because I want to store in a map format the variable name the string last name String is adult Boolean in age end so the key is going to be the variable name the value is going to be the data type so I'm just going to have string dynamic equal to an empties map and of course we need to name this so I'm going to name this Fields also don't forget to overwrite this because this is coming from the super class so let's comment and write what we want because this logic can get tricky not too tricky but can get tricky so so the variable name first name is going to be of the type string last name of the type string age of the type end so we want to make this map right here so we can just do map more like fields at the rate now the key is the variable name so this can be element dot name because it will visit the field element the name of this field element is element dot name right and this will be equal to element now we want to get the return data type of this field element so it's going to be similar to what we had here right element dot return type but it doesn't have anything known return type so can we type try yep it has something known as type so we can use that dot to string again the types here are returned like string star it doesn't return just string it has an asterisk after that so we want to replace asterisk with an empty string and that's it that are logic we can remove this comment so every time it encounters a field element it's going to save it to our map of field so this was the logic for the model visitor it's going to fill out the class name and the fields so we are not worried about that so this was kind of like the help of function or a helper class for us here we are going to have the main logic and we are going to call that let's say Json generator dot dot to be honest I don't have any other name for it you can write something else but even here what we want to do is have class gin Json generator and it will extend generator for annotation what is this it's going to have a generic type as well but what is this so after we run the command that I mentioned to you here after it is run the build Runner is going to check the build.yaml file and all of that and it will go down all the files in the lib folder and see if The annotation is present there so if there's person and our annotation is present there this class will get to know it and one overwritten function will run and that will be the main starting point or the logic for our application so here we just need to mention what annotation if it finds should this function run so The annotation is Json generator annotation we'll import that from annotations annotations dot dot this is this file right here from the annotations Library so every time it finds an annotation with this Json generator annotation it will run this function which is generate for annotated element now we can have a return data type of void present here it doesn't return anything and this is going to have all the logic for our app again if you want to hide the warnings you can ignore implementation Imports for this file and even this one and as in function name your mentions generate for annotated element we want to generate code for the annotated element we just found and what should the code be well first of all we want to make sure that our model visitor goes through that particular class and this will help us with that so we are going to have an instance of visitor year which is model visitor so we have an instance of this class and now we will use this element we will use this element so that we can call a method on this called visit children and pass in the visitor so this element will pass visit all the children with the help of this model visitor and this model visitor will call all of the methods and when it calls these two methods are instance variables will get filled so class name will get the class value and the fields will have the field values now we just want to create the logic so what do we want to write in the generated file so we are going to have final buffer equal to string buffer this buffer will help us write lines in a file and actually this should not have a return data type avoid we want to have a return data type of string so generate for annotated element will generate what what should it return from there so it's going to return a string which should Encompass the entire person.g dot dot file that is generated so if you just type it on highway or a person.g dot dot file will be created with high return in there but actually it won't be created the reason for that is two things here we need to add a new thing called part but we look at that in a bit and the other thing is this is not a correct Syntax for Dart file whenever we return something from here the syntax should be correct it will be checked when we run the Builder command that I mentioned over here it will check if the syntax is correct and only after that will person.g dot dot be created this is a good functionality because whenever we generate some source code now we can be sure that this file is going to have no errors at all anyways right now we are not returning anything let's get to the logic your buffer will give us pro access to a property called writeline and with this we can write what do we want to write now what I'm going to do is just take this person class I'm going to right click over here and open to the side so now in the side I have the class that I want to generate it's going to look like this so I have a reference of what I want to do of course I wrote all of this right now just so that I had a reference and there was no error now let's see what I want the first line is the class so I want to write class and then the class name how are we going to get access to that well here we can just create class name equal to visitor dot class name so we visited the children so we visited Constructor element and visited field elements now this both are populated so it has the correct last name and the fields I can use this class name but this is not my class name my class name is going to be person with generator added after that so it's going to be class person generator if I have class person again it will cause errors in our application because there are two classes we can't have that so this is person generator and now we can have class dollar class name with apparent thesis I can just move this to the bit and now I can copy this again paste it here and have a closing brace so I have class person generator with the brace and an ending brace after that I can have buffer Dot rightline and have final now I want to get access to the string and then the variable name the data type the variable name the data type the variable name so this is a good indication of using a for Loop because I don't know how many fields there are this class has four Fields but another class marked with the same annotation can have 10 different fields so I want to reach all of them so I can have 4 int is equal to 0 I is less than visitor Dot Fields this right here dot length I plus plus now it can have buffer dot right line and if you're wondering what this right Ln is basically write on a new line if you are from java you might know system.out.print Ln new line and now we have final now I want the data type first so data type is the value right in our map we have stored it as a value the key is the name of the variable the value is the data type so we are going to have dollar visitor Dot Fields dot values this is how I get the values but I want it for this element because we are in the for Loop actually we went out we want it to be in this for Loop so it cannot be values because values gives an iterable so we will use dot element at I if you didn't understand let me explain it to you again we have final then we have visitor Dot Fields which is the map dot values will give us the value which is the data type we have stored like that in our Fields variable right here the variable name is the key the data type is the value so we have values dot element at since we are in a for Loop we don't necessarily want the zeroth element or the first element or the second element in the map it can be anything so based on this index I want to write it so after we have that it's going to have final string Boolean integer whatever and now we need the variable name so it's going to be similar to this again so we have visitor Dot Fields but this time keys not values because the variable name is present in the keys dot element at I I hope you understood this this is a bit difficult to understand but if you did congratulations if not ask me in the comment section below and I'll help you understand it even more so this means we have written all of these final Fields now we need to generate a Constructor so I can just have a comment here if you want you can create a new method for it it will be much cleaner but I'm too lazy to do that and for the Constructor again I'm going to have buffer dot right Ln constant then the class name sub dollar class name then a parenthesis a curly brace so that I can have named arguments I'll close this so as soon as I have a bracket there I'm just going to close it right away and then again a for Loop so I'm going to run this for loop from 0 to fields.length and then I'm going to have required this Dot and there is no data type involved here so I think I can remove this field entirely fields.values Dot element.i all I need is Fields dot Keys Dot element.ie and after that make sure to add a comma if you don't add a comma it will not generate the file for you because this is a DOT syntax problem and after that save it and now we have a Constructor created as well the next thing we need is let's say a two map function so I'm going to have buffer dot right Ln I hope you're getting the hang of this so we have map string dynamic so whatever static part is there the normal boilerplate code we are going to have that and after that whatever non-boilerplate code is there generally for Loop is used in this part right here so this model visitor just calling it once the visit children gives us access to everything and then it's just copying this nicely and figuring out the error once you get it of course you're going to get it now we are just going to have buffer dot right Ln I want to create a return and I want to close this right away again this can be a bit confusing it took me a while to write this entire thing out with many errors especially with the ones that include comma then we again have buffer dot write Ln and your I think we want to return all of these fields so again a for Loop since we are writing for loop again and again I think we can create a function for this but anyways keeping the code clean is up to you I'm not keeping it I would highly recommend you to do so but now we don't want a single inverted comma we want a double inverted comma the reason for that is over here you can see these are single inverted I have also wanted single inverted so this is how I'm keeping it and now we don't want required in this part we just want visitor.field.keys at elementi like this in a string and then like this without the string right so we have a string so whatever keys are there the name in a string and after that without the string and this is inside a for Loop so it will generate that and obviously don't forget the comma and I think you don't need a semicolon here you need a semicolon yeah because this return is going to have a semicolon you can see that but this does not have a semicolon and this is for this one I think I hope I'm right let's see let's wait till the end and see what the conclusion is when I was writing the code for this after every single function I used to test it out but I'm going to keep it this way so that we have suspense of what's going to happen now I'm going to create a form map and we are going to have buffer dot right Ln this time a factory keyword and then I want the class name to be present so I'm just going to have dollar class name Dot from so we have ocean Dot from map and then the normal boilerplate so we have map string dynamic like this also don't forget to name this map and we'll again write this down but we only want a closing brace then we are going to buffer Dot right Ln return dollar class name with apparent thesis and we are going to create close the parenthesis as well so we have like this something like this pressure now we are going to buffer dot write Ln the name of the field so again we need a for Loop I can type this for Loop right here and here I need the key so it's not going to be in a string format here so we have suppose name here this will give access to name and then I want map add name so just gives me access to map at name and of course the comma so we have the from map created as well now the final is the copy with function so let's have copy with created we are going to have buffer dot write Ln then we are going to have dollar class name copy with like this and actually I would highly recommend you to try this on your own so that you build some confidence because logic is the main part once you have the logic figured out you don't have to care about all of this stuff you can find this in the my GitHub repository Linked In the description below you can start off with that but the main part is the logic so you have to be clear of what you want to do in your app so after having copy with you also want to close it and this is going to have like this so this is this part copy with and then you have this right here inside of this you want a for loop again but this for Loop is similar to what we had right here so I'm just going to go ahead and copy what we had in the start this for Loop come down put it in the copy width I'm going to remove final because there's no final year this whatever data type there is we'll add another level property there so if this is nullable because copy with function has that is the user doesn't want to enter the field they cannot so we have that and then the variable name perfect don't forget a comma here and now it strikes me that we have not added a semicolon after this so we want to add semicolon as well otherwise an error yeah you have to remember all of that so I think the parameters for the copyright function is done I also want to close this bracket that we just have so this is this part right here then this part is all of the parameter arguments then this is this and we are closing this similar to this now this is going to return a person class and we have done that before for our forward map from map so I can just have this logic written right here so it's going to return a person class then it's going to have the name property which is keys and then we don't want access to map it's just going to have this element at I so name this will give us access to name because it's keys this will also give us access to name so Name colon name and then we just want if it is null then we want this Dot name again so I just want to copy this and put it out here a little too confusing but if you understand this this will give us access to name then we have a colon then we have name again and then we are going to check if it is null if it is null then just take this otherwise this dot name again so it again is this thing right here and of course the comma and I think these are all the things that are needed for our buffer and all of our file names copy with two map from map if you want more functions like Json and code decode and all of that you can add those but now I think you got the basic idea of what's Happening here now we just want to do return buffer dot to string so whatever we have written here we want to convert it to a string and return it now I can come to the generators dot dot remove this calculator class and here let's close the side panel as well now we are going to have Builder return here Builder and if you go to the build.yamlier we have Builder factories right these are containing the topmost level functions and these are going to have a type definition like this that's exactly what we are going to do we are going to have Builder we're going to name this Json generate Json class if it's not matching with any of the values that you've mentioned in the list it will throw an error so if you decide to change the function name make sure to add the changes here so you can have another class name or whatever but it's going to be called generate Json class for me and then we are going to build our options options and we're going to return a shared part Builder if you're wondering what a shared part Builder is it is a builder which will generate content for part of files what are part of files you'll get to know when we generate one file just wait for it just wait for some time and you'll understand what part of files are let's just import Builder and we just have to remove material dot it's coming from the build package here now we need to pass the generators so we have shared part builders with a generator passed to it and this is the Json generator package it requires a list of generator and Json generator extends chain data for annotation package this generator for an annotation extends generator so this is ultimately a generator and then we need to pass in the part ID this part ID will help us to ensure that multiple shared part Builders can produce non-conflictive part of files this is kind of like a distinctive uid which will help the share multiple shared part Builders to produce non-interfering part of files you can put it that way and then we have Json underscore generator so this is the unique name for this basically our files will be prefixed with this so if we have person it will be person dot Json underscore generator dot g dot dot or I'm not sure about the dot it can be like this or like this either of them but it will not be the name of the generated file if we have an error it's going to be shown in the terminal that it's going to have Json generator dot dot you'll see that if you are concerned about the tests error you can remove them we are not writing any of them if you want you can write it for the generators anyways now we need to test the application so let's see if it is working so we'll go to the example file or the folder sorry then in the puff spec.aml file we are going to import some of these so we need three things in the dependencies we are going to have annotations so that we can annotate the person class right here right now if we just remove everything and bring it back to where it was earlier we don't want a reference anymore we want there to be at the rate annotation that we just created right so we are going to import that so we are going to have path dot dot slash annotations and let's have some spacing so this is the dependency in the dev dependency we are going to have two things build Runner so that we can run the commands to generate the file and then we need this generator so it's going to be generators path dot dot slash generators so if you've used Revo pod generator or riverpod annotation The annotation is used in the dependency and riverpod Generator is used in the dev dependency right simple now we can come to the person class and use The annotation let's see what the annotation name was it was Json gen so let's have at the rate Json gen you can see we can import it and it Imports annotations annotations not DOT perfect now all we need to do is run the command that I mentioned to you earlier flutter pub run build runoff so this will run the build run or package and we want to build and we want to dash dash delete the conflicting outputs so whatever so suppose a person dot g dot dot file is already created we want to delete the conflicting file the person.g dot dot file and create a new one so this will help us with that and if you want you can add watch over here so that the next time things happen it will happen much quicker and now just click on enter and this gives an error the reason for that is we are in the code generation package we want to migrate to the example folder which is the flutter folder and then we want to run this because the code generation package has three subfolders it doesn't have pubspect.aml file and we cannot run build Runner right here we want to add that in example folder because we want it to migrate through all the files of lib and see if Json generator annotation is there or not now we can run this here you can see it is succeeding that means we don't have any error in our buffer reader congratulations to us but it's not generating the file the reason for that is we don't have something known as a part file here we need to include what is the part file here if you've used River pod or retrofit or anything you have to include a part file viewer and then you have to run this command and now after you do this you can see person.g dot dot is created classes person gen it has everything the two map from map and copy with functions this means it is working everything now works of course the errors here don't go away but you've generated the class now if you want you can just comment this out and use the person generator class everywhere but if you just look over here there's a part of file I told you the shared part Builder helps in creating part of files so this person.g dot dot file is a part of the person.dot file and here we are mentioning that this course in class have a part called person.g dot dot now if you don't understand this let's go to the my home page and here I'm going to create in the build method something known as person generator person generator which is equal to person generator okay if you come up here you can see we are importing person.dot file not person.g dot dot file if you do that it will give an end of error because the imported Library so and so cannot have a part of directive so we just have person.dot file here and that's exactly what a part and part of file is now if you want to see the magic let's delete this let's also delete this now we are sure that this class is going to run uh we have we are not even going to check that if you want you can check it out in your own project but anyways let's come back to this we're going to activate all of these methods or properties we're going to create a new file let's call this order.dot so what is our order going to consist of there's name of the product there's name of the ID of the product or the ID of the order is going to have the list of string of name of products this was the name of the order my word and let's say a double of amount like the total amount what is it and now let's run this command so we are running the code generation package we remove the part file for some reason I don't know why so let's add that again question dot g dot dot and let's run this now you can see a person.g dot dot file is created but not an order dot g dot dot the reason for that is we have not annotated this class so you need to annotate this class in order for it to get identified and this command is not only running for our annotation if you have a riverpod annotation here it will run that as well now again I can just have at the rate Json generator I forgot to put a part file so let's have part order dot g dot dot and run it again now you'll see it's much quicker if you add watch here it will be much quicker then next the second time or the third time you build it because it will contain the cash in it if you don't use watch then second or the third time you use it it's still going to take a lot of time but anyways we have order.g it has all the correct amount stuff written in it and the person.person.g which has everything correctly written now if you're still confused of how all of this is happening let's take a brief run off how all of this stuff happens so I run this command okay the build Runner gets activated so the build Runner calls the build.yaml file it sees all the configuration it sees the import line here which is generators dot dot so it will go to this generators and see if it has something of this definition if it has great if it's not it's going to throw an error that the property name is different or the method name is different or something like that if it's all good then we are going to create a shared part Builder shared part Builder helps in the part and part of files then we are going to have the Json generator there and you can see it the generator for annotation it whenever it identifies Json generator annotation it's going to run this method and here we have a visitor here so after we get the visitor we are going to visit the children so it's going to visit all the children of the annotated class so for our person class it found this it's going to go over all of its children so it's going to go away the Constructor so the Constructor Returns the person class then the field so if there's string name and we store that in the variables so for the class name we store it in the class name variable and the fields is in the fields variable and then we write out the entire class for it and return the buffer.2 string and based on that it will return the person.g dot dot and this is how helpful the build.aml is we have told it the import line we've told it the build of factories what method is over here what output should the extension be of the bill to Cache so whenever we use shared part Builder we want build to cache and in the applies Builder combining Builder to be present so that it can combine all of the shared part Builders together and one function or in one file so I hope the whole thing was clear to you if you have any questions or if this video wasn't clear to you let me know in the comment section and I'll make make sure to help you out with it if there are many people who didn't understand this and make sure to give a second ride to this video because this is quite an interesting topic and this will help you to contribute to the community because code generators are there very less and there are a lot of tasks a lot of repetitive code to be written where code generators can be used and there are not many resources online where we can get to know how to do all of this stuff so let me know I was just demonstrating to you how you can use or create your own code generation tool if the basics are clear you can create your own code generator tool and work it for you if you want to upload your code generator to a DOT package published To None will have to be removed so you'll have to remove this so annotations which will give an error now so if you're getting this error you want to make sure that this annotations package is also uploaded on pub.dev that's what Riverport does right or you can just have annotations in this folder itself the generator folder in the SRC you can just have annotations and use that annotation here but for me I'm just using it locally so I'm going to have publish to none if you're wondering why you would want to use this code generation tool if it's not clear when there's a lot of repetitive code to be written for example there are a lot of classes that need these functions every single time that's where code generation can help us this will reduce a lot of time it took a lot of time to write but once you write it you can create hundreds and thousands of classes be sure about the consistency the code writes and be sure that this code is always going to be right and of course if the dot syntax changes anytime you can just go to your code generation tool update the syntax there rerun this command and all the hundreds of classes will have the new DOT syntax with them this is where code generator can help us so this was it for the video thank you so much for watching see you in the next video
Info
Channel: Rivaan Ranawat
Views: 11,606
Rating: undefined out of 5
Keywords: flutter, flutter tutorial, flutter tutorial for beginners, flutter beginner tutorial, flutter code generation, flutter automation, flutter automatic code generation, flutter code generator, flutter from scratch, flutter automated, dart tutorial, dart, dart code generator, flutter package, dart package, code generator, code generator tutorial
Id: 77a3KTIRPaw
Channel Id: undefined
Length: 48min 26sec (2906 seconds)
Published: Wed Dec 28 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.