Streamlining code generation using build_runner for the app and it’s local packages

Virender Verma
2 min readJul 4, 2024

Introduction to build_runner

In Flutter projects, code generation can significantly simplify development by automating repetitive tasks. One powerful tool for this is build_runner. build_runner is a package that automates the process of generating code for your model class. It works by running "builders" that you configure to generate files such as freezed classes for immutable data structures, JSON serialization code, and more. By using build_runner, developers can focus more on writing business logic and less on boilerplate code.

The Challenge

I was working on a Flutter project that relied on a local package, which is using build_runner to generate freezed classes. The challenge was that I had to navigate to the local package directory and run the build_runner command there, which was inefficient. This manual process was cumbersome and inefficient

I wanted a solution that would allow me to generate files for both the main project and the local package by running the build_runner command from the root of my main project. After some research and experimentation, I found a way to streamline this process by creating build.yaml files both in the root directory of my main project and the root directory of the local package.

Step 1: Create build.yaml in the Main Project

First, I created a build.yaml file in the root directory of my main project. This file instructs build_runner to generate code for all Dart files in the lib directory of the main project as well as the subpackage.

targets:
network_package: # Your local package name
sources:
- network/lib/**
$default:
sources:
- lib/**
dependencies:
- :network_package # Your local package name
builders:
freezed:
generate_for:
- lib/**/*.dart

Step 2: Create build.yaml in the Subpackage

Next, I created a build.yaml file in the root directory of the subpackage (network_package). This file ensures that the freezed classes are generated for all Dart files in the lib directory of the subpackage.

targets:
$default:
sources:
- lib/**
builders:
freezed:
generate_for:
- lib/**/*.dart

Step 3: Update pubspec.yaml Files

I made sure that both the main project and the subpackage had the necessary dependencies in their pubspec.yaml files. Here’s an example for the main project:

dependencies:
flutter:
sdk: flutter
network_package:
path: ./network_package

dev_dependencies:
build_runner: ^2.4.11
freezed: ^2.5.2
freezed_annotation: ^2.4.1

Step 4: Run build_runner

flutter pub get && dart run build_runner build --delete-conflicting-outputs

Conclusion

By configuring build.yaml files both in the root directory of my main project and the local package, I streamlined the code generation process. This approach allows me to run build_runner from the root of my main project and have it generate code for all dependencies, saving time and reducing complexity. If you’re facing a similar issue, give this solution a try and enjoy a more efficient build process in your Flutter projects.

--

--

Virender Verma
Virender Verma

No responses yet