Supercharge Your Flutter Development with Mason
You know that feeling when you're itching to build a cool new feature, but first you've got to slog through all that boilerplate code? Yeah, that used to be me. Every. Single. Time. I'd get excited about adding something awesome to my Flutter app, then remember all the repetitive stuff I'd have to write first. It was such a buzzkill. But then I found Mason, and it's like someone handed me a fast-forward button for the boring parts. Now, I can focus on the fun stuff - actually building features that matter. In this article, I'm going to show you how Mason changed my coding life. If you're tired of writing the same old code for every new feature, stick around. You're gonna want to hear this.
Understanding Domain-Driven Architecture in Flutter
Before I get into the cool stuff about Mason, let me quickly tell you why I'm such a fan of Domain-Driven Architecture (DDA) in Flutter. It's like this neat way of organizing your code that actually makes sense for the business you're building for. Instead of having a jumbled mess, you've got everything neatly separated - your UI stuff, your business logic, and all that data handling. It's been a game-changer for me. I mean, when I'm knee-deep in a complex app, being able to navigate my own code without getting lost is pretty awesome. Plus, when it comes time to make changes or add features, I'm not pulling my hair out trying to figure out what goes where. It's become my go-to approach for Flutter projects, and honestly, I can't imagine coding any other way now.
Introducing Mason: The Boilerplate Slayer for Flutter and Beyond
Mason is a powerful code generation tool that allows developers to create templates (called "bricks") for repeating code patterns. For Flutter developers working with DDA, Mason is a game-changer. It enables you to quickly scaffold new features while maintaining architectural consistency.
My Journey with Mason
As a Flutter developer, I found myself spending an inordinate amount of time setting up new features. Each new component required creating multiple files across different layers of the architecture. It was tedious and error-prone.
Then I discovered Mason. After setting it up and creating a custom brick for my DDA feature structure, I saw immediate results. What used to take 30 minutes of setup now takes less than a minute. The consistency in my codebase improved dramatically and it felt refreshing and easy anytime a new feature was to be addded to a project.
Getting Started with Mason
Here's a quick guide to setting up Mason by building the feature brick I use to speed up my development process.
Install Mason CLI:
dart pub global activate mason_cli
Initialise Mason in your project:
mason init
Create a new brick for your DDA feature:
mason new feature_layer
Define your brick's template files to match your DDA structure.
The beautiful advantage of Mason is it's ability to help you not generate code but also file directories. You can literally generate your entire project directory as a template with boilerplate code in the files you need. Pretty awesome right?
Above is the template structure of my boilerplate code for creating new features. These are created in the __brick__ directory in your mason brick. Once you run the mason command to make a feature, the entire directory with all the files and code is generated in less than a minute, a process which usually took way longer. A breath of fresh air.
Every feature has a name so we need to modify our brick.yaml to request for the feature name. You can see that in the code snippet below.
name: feature description: Generates a feature which makes up the application, domain, presentation and infrastructure layers. # The following defines the version and build number for your brick. # A version number is three numbers separated by dots, like 1.2.34 # followed by an optional build number (separated by a +). version: 0.1.0+1 environment: mason: ">=0.1.0-dev.57 <0.1.0" vars: name: type: string description: Feature name prompt: What feature do you want to create? default: DDD_Feature
That's really it for setting up. Now before you can use your brick, you need to add it either locally or globally. I prefer globally so I don't need to have it restricted in a particular directory.
mason add feature --path ./feature_layer -g
Use your new brick to generate features:
mason make feature_layer
Code Showcase
Best Practices and Tips
Keep your bricks modular and focused
Use variables in your bricks for flexibility
Regularly update your bricks as your architecture evolves
Share bricks with your team for consistency
Conclusion
Mason has significantly improved my productivity and code quality in Flutter development with domain-driven architecture. It's eliminated the tedium of setting up new features, allowing me to focus on what matters—building great apps. I encourage every Flutter developer to give Mason a try and experience the boost in productivity for themselves.
Are you ready to supercharge your Flutter development? Give Mason a shot, and let me know in the comments how it transforms your workflow!
Here's my repo, you can check it out and modify it to fit your preference.
Have fun and all the best!
Subscribe to my newsletter
Read articles from Ronald Nettey directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Ronald Nettey
Ronald Nettey
A simple person who just loves to create, tech happens to give me that privilege to express such creativity and to bring ideas to life. I'm not here to impress anyone but to be of value and help to as many.