iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🐙

Next-Level Flutter Development: Managing Personal Bricks with a Dedicated Mason Repository

に公開
2025/06/12

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):

  1. Register with BrickHub (BrickHub specification)
  2. Manage in a dedicated GitHub repository (git specification)
  3. 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
mason.yaml
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.

mason.yaml
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 features directory under lib.
  • Under the features directory, there is a directory for the feature name (shown as "hoge" in the diagram).
  • Features are divided into three directories: data, model, and ui.
  • In the initial state, it generates hoge_repository.dart, hoge_state.dart, hoge_notifier.dart, and hoge_screen.dart files.
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.
https://github.com/K9i-0/mason/tree/main/bricks/feature

Key points:

  • Use the name received in vars for paths, filenames, class names, etc.
  • Use .snakeCase etc. to convert to the appropriate case.

Trying It Out

I tried using the brick I created in the following repository.
https://github.com/K9i-0/mason_sample

  1. Edit mason.yaml as previously explained.
  2. Run mason get to retrieve the brick.
  3. Run mason make feature to execute the brick.
  4. 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.

https://zenn.dev/k9i/articles/70798511bac2b9

Meaning of "mason"

Mason apparently means a stonemason. A familiar term would be the "mason" in Freemasons.

https://ja.wikipedia.org/wiki/フリーメイソン

GitHubで編集を提案

Discussion