I’ve been working with Flutter for a while now( almost 8 months you could say). Almost in every project, I’ve tried my best to follow the BLoC pattern after getting exposed to it. Before that, managing states with the `setState()` method was becoming hard for me. And learning redux seemed complex also. Anyway, if you don’t have much idea about what BLoC pattern is, you can watch this video to get the basic idea of it:
So, as my project size was getting bigger the more I had to write a lot of boilerplate codes for different blocs. And it was getting out of hand 🤕. So I thought is there any way I could automatically generate these codes? Then decided to read more about writing console application using dart. 🤔 And created a console application to help me generate the skeleton of the blocs.
So, the console application that I wrote is hosted on GitHub.
You will have to clone it first, and don’t forget to run `pub get` after cloning.
You can activate this repository on your local machine by the following command:
After running this, you will see texts like this in the console:
Activated bloc_code_generator 1.0.0 at path “<path to the cloned project>”.
Now, to run the main function you gotta use the following command:
This will run the main function hence, the code generator. It is a console application, remember? You can pass arguments too. I’ve decided to use 3 arguments in total. All of them are optional. You can use the following command to see what are those arguments and what do they do:
So by now, you can see that you can explicitly set the name of the file, path where it will be saved (for example you can set it to your project folder. Running from the project folder will save it there by default though) and if you want to add the comments in your code (I added comments for you to understand the generated code better, but you can disable them).
A sample command would be like this:
Here the bloc file name is set to ‘MyBloc’ and the path is set to where I wanted to generate it. And — no-comments means there will be no extra comments in the generated code. If you don’t pass any arguments, then it will use the default values for each of them.
You will see this text after running that command :
Generated file myBloc.dart at File: ‘E:\AngularDart\bloc_code_generator\myBloc.dart’ without comments
The generated code will look like this:
I commented out the everything that requires rxdart. if you want to use rxdart then just uncomment them and comment the ones above them 👍 Also, you can generate the code with comments for better understanding!
InputMap helps us to take input and understand what to with it. The value can be any type of value, but I strongly recommend to use a specific type of value other than dynamic. And the event part helps us to understand what kind of event( like should we increase or decrease for a simple counterapp case).
OutputMap helps use to pass the value to the outputStream. It holds the output value and the state( for example if you pass state as loading, you can handle show Progress Indicator using StreamBuilder). You can use any kind of states according to your need. Just add your values in the enumerations Event & ScreenState.
I created the counter application that you get after creating a new flutter project but using this auto-generated code.
Voila! The bloc file is working fine 😄 Now, don’t forget to deactivate the package after being done using it.
Deactivated package bloc_code_generator 1.0.0 at path “E:\AngularDart\bloc_code_generator\bloc_code_generator”.
You will see this message after deactivating the bloc_code_generator.
I’m loving dart and flutter more and more ❤️. It was really fun to code this console application. It helps me by saving a little bit of time every time I need to code some bloc for my projects. Just need to run the command from the location where I want to save the file then it will save the files right there 😁 This is my first article, so please ignore the mistakes. If you have any suggestions or having difficulties to understand then comment here!
Github : https://github.com/adar2378/
Email : email@example.com
*used https://carbon.now.sh to generate these cool code snippets