"Generate Better Code in Seconds with Riverpod and Flutter!"

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone assalamu alaikum here we'll see how to use reverbot 2.0 and Riverport generator package to Auto generate our providers now here I have a provider and this provider just returns a string and the provider name is string label provider and I have the provider over here and then what we do we just grab the provider and use the watch function to display it on the UI and that's the basic old way of doing things but now we will see how to use it how to generate this provider this provider using our real report generator package and which is actually better than riverpod old versions because this way it separates these providers and it separates the provider in a different class and which is more maintainable I think anyway so to install that first go to your pub spec.yml file and over here first thing you have to of course make sure that you have register workload the version should be 2.0 or above and then you also have to have this two plugins build Runner and Ruby per generator so this one actually does the auto generates the code and this one is for running the generated code and get organized now over here let's come over here now our job is to turn this provider into a new Reverb provider the way Riverport 2.0 works okay now the first thing I'm going to do over here right here I'm going to have this annotation now what is this annotation first we have to have this River pod so with this it tells you okay generate riverpod code and after that you have to mention what a river part should return now we know that our River part over here returns a string type okay so let's go ahead and do that so over here mention the stream type and then you have to give it a function name you can give it any kind of name you want it doesn't really matter so here I would say string label provide string label that's it and then after that because this is a function so it takes a parameter so here you have to also mention the parameter type and the name so it is string label and the ref it has to be like this and then after to that the object name is ref and then inside this you can return anything you want so of course here we want to return hello world the string because this is the string we want to return now that's the first thing and another thing I said that okay this Auto generated providers are generated in a separate file so where is the file and how to name it now it's named after where this function is located now we know that this function string label is located in main.dart so you have to give it a name for generated provider based on this one say for example this this function resides in users.dart so your generated provider would be also in similar kind of names okay so let's make it clear over here so we know that this one over here resides inside main.dart so what we would expect over here it's like this so here we have to mention like part and then here we do main dot g dot Dart so it tells us it tells the reverbot generator okay generate reverbot providers and the file name is main.g.dart and put the generated code there but why the file name is main.g.dart because the original function that is also in main.dart so generated file and original find they have to have this similar prefix and the suffix is the middle part is a bit different over here we are using G that tells you okay put the file in main.j dot Dart file okay so that that's how it works so that's how you have to give the name one more time so if your file name was user.dart over here you have to give it user dot g dot dot like that okay so it all depends where this function is located okay now the last step now over here we have to create we have to run the command so what is the command so go to your terminal and run this command so flutter pub run build Runner watch and delete conflict output okay so we have to run this one and hit enter right so with this hopefully it will generate uh provider but here we do have a mistake so here it is here string label l-a-b-e-l but here we have ble so we have to correct the name make sure the name is correct and the name format now we'll cancel this and then we'll try to run it one more time all right okay and it's running the build it's still running all right so before we go ahead and run we have to comment this out otherwise we'll get error because this is our old provider now anyway the a new file has been generated so what is the file the file name is of course main.g.dart and it would be located as same as in this file where it is located so this file is located under lib folder so this file would also be located under lib folder so this is the file that we're talking about let's go ahead and check that out and at the bottom over here we see that we have a provider generated for us the provider name is string label provider okay so that's how providers are Auto generated using riverpod 2.0 Riverport generator now actually we are ready to use now we can go ahead and restart the app and of course nothing changes and over here what we could do we can just simply go ahead and change the text we can say hello world of riverpod hello world of riverpod all right so let's restart our app and we'll see that it took actions all right so make sure that you have the provider over here as well now of course this is much better because a lot of the providers are Auto generated and they are in a separate class and this is wonderful now what about this object string label ref now string level ref is how to disposal provider type what it does actually tells you that this is a provider this is an object that providers use is to interact with other providers so if you wanna if you have a lot of other providers and you want to interact with each other you should use this one I mean the one that's generated for you of course the name could be very different now of course with this reward 2.0 everything looks much better and a little bit more simpler thank you
Info
Channel: dbestech
Views: 7,854
Rating: undefined out of 5
Keywords: riverpod auto generate code, flutter riverpod 2.0, flutter riverpod tutorial, flutter auto generate code, flutter riverpod state management, flutter tutorial
Id: xcxlc6ZagRc
Channel Id: undefined
Length: 6min 33sec (393 seconds)
Published: Sun Mar 26 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.