iTranslated by AI
Next-Level Flutter Development: Managing Personal Bricks with a Dedicated Mason Repository
Introduction
By using the mason package, you can easily generate code based on templates called "bricks". (I recommend this article for an explanation of mason itself.)
In this article, I will explain how to manage personal bricks using a dedicated GitHub repository for mason, while also covering specific ways to write bricks.
Article Overview
- How to manage personal bricks using a dedicated GitHub repository for mason
- Practical ways to write bricks
- Examples of actually using the created bricks
Managing Personal Bricks with a Dedicated GitHub Repository for mason
It seems there are three ways to manage bricks (ways to specify them in mason.yaml):
- Register with BrickHub (BrickHub specification)
- Manage in a dedicated GitHub repository (git specification)
- Manage in the same repository as the Flutter project, etc. (local path specification)
Each has its pros and cons, but this time I will introduce method 2, "Managing in a dedicated GitHub repository."
Specifically, prepare a repository like the one below (hereafter referred to as the "mason repository").
https://github.com/K9i-0/mason
In the mason repository, we will create a directory called bricks and store multiple bricks inside it.
Suggested use cases for each management method
Register with BrickHub (BrickHub specification)
- General-purpose items intended for a broad audience
- Note that names must be unique within BrickHub
Manage in a dedicated GitHub repository (git specification)
- Items for individuals or teams
- When you want to use them across multiple projects
Manage in the same repository as the Flutter project, etc. (local path specification)
- Items for individuals or teams
- When you want to use them within a single project
Usage Configuration
To use a brick managed in a GitHub repository, describe it in the mason.yaml file as follows (example for a brick named feature).
- Specify the GitHub repository URL in
url - Specify the relative path within the repository in
path
bricks:
feature:
git:
url: https://github.com/K9i-0/mason.git
path: bricks/feature
Brick Version Management
When considering brick updates, version management becomes desirable.
In this case, we will manage the version of each brick using git tags. To manage the versions of multiple bricks individually, we use the following format for tag names, including both the brick name and the version name:
- feature-v0.1.0
- Feature name: feature
- Version: 0.1.0
Since mason allows you to specify a tag using ref, add the ref to the previous configuration.
bricks:
feature:
git:
url: https://github.com/K9i-0/mason.git
path: bricks/feature
ref: feature-v0.1.0
Practical Ways to Write Bricks
Now, let's look at how to write a brick.
Challenge
This time, we will create a brick that generates code for a feature-first directory structure, satisfying the following conditions:
- There is a
featuresdirectory underlib. - Under the
featuresdirectory, there is a directory for the feature name (shown as "hoge" in the diagram). - Features are divided into three directories:
data,model, andui. - In the initial state, it generates
hoge_repository.dart,hoge_state.dart,hoge_notifier.dart, andhoge_screen.dartfiles.
lib
├── features
│ └── hoge
│ ├── data
│ │ └── hoge_repository.dart
│ ├── model
│ │ └── hoge_state.dart
│ └── ui
│ ├── hoge_notifier.dart
│ └── hoge_screen.dart
```text
:::details Details of the generated files
The contents of each file are as follows:
```dart:hoge_repository.dart
import 'package:hooks_riverpod/hooks_riverpod.dart';
final hogeRepositoryProvider = Provider<HogeRepository>(
(ref) {
return HogeRepository(ref);
},
);
class HogeRepository {
final Ref _ref;
const HogeRepository(this._ref);
}
```text
```dart:hoge_state.dart
// ignore: unused_import, directives_ordering
import 'package:flutter/foundation.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
part 'hoge_state.freezed.dart';
@freezed
class HogeState with _$HogeState {
const factory HogeState({
required String id,
}) = _HogeState;
}
```text
```dart:hoge_notifier.dart
import 'package:hooks_riverpod/hooks_riverpod.dart';
import '../model/hoge_state.dart';
final hogeNotifierProvider = NotifierProvider<HogeNotifier, HogeState>(
() => HogeNotifier(),
);
class HogeNotifier extends Notifier<HogeState> {
@override
HogeState build() {
return const HogeState(id: 'id');
}
}
```text
```dart:hoge_screen.dart
import 'package:flutter/material.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
class HogeScreen extends HookConsumerWidget {
const HogeScreen({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
return Scaffold(
appBar: AppBar(
title: const Text('Hoge'),
),
body: const Center(
child: Text('Hoge'),
),
);
}
}
:::
Writing the Brick
For details, it is easiest to look at the actual code.
Key points:
- Use the
namereceived invarsfor paths, filenames, class names, etc. - Use
.snakeCaseetc. to convert to the appropriate case.
Trying It Out
I tried using the brick I created in the following repository.
- Edit
mason.yamlas previously explained. - Run
mason getto retrieve the brick. - Run
mason make featureto execute the brick. - Answer when prompted for the feature name.
With these steps, I could easily generate the feature code.
Summary
- Explained how to manage personal bricks using a repository.
- This method seems useful when you want to use the same template across multiple projects. Bricks are handy for things like generating multiple files.
- While I used a feature-first structure as an example, a layer-first structure should work fine as long as you specify the paths correctly.
- I've omitted much of the basic mason usage, so please feel free to comment if you have any questions.
Snippets are Recommended for Templates Contained within a Single File
Meaning of "mason"
Mason apparently means a stonemason. A familiar term would be the "mason" in Freemasons.
Discussion