← Back to posts Cover image for Mastering Code Generation in Dart & Flutter: Building Efficient & Maintainable Tools

Mastering Code Generation in Dart & Flutter: Building Efficient & Maintainable Tools

· 4 min read
Weekly Digest

The Flutter news you actually need

No spam, ever. Unsubscribe in one click.

Chris
By Chris

Build Your Own Dart Code Generators: A Practical Guide

We’ve all been there. Your Flutter app reaches a certain scale, and suddenly you’re drowning in repetitive, error-prone code. Manual JSON serialization, verbose data class definitions, or complex state management boilerplate can slow development to a crawl and introduce subtle bugs. This is where code generation shines. By automating the creation of this repetitive code, you gain type safety, consistency, and the freedom to focus on your app’s unique logic.

Building your own generator might seem daunting, but the process is a rewarding investment in your project’s health. Let’s break down how to get started and avoid the common pitfalls.

The Core Toolkit: build_runner and source_gen

Dart’s ecosystem provides excellent tools for code generation. The primary pillars are the build_runner package, which orchestrates the build process, and source_gen, which offers utilities for parsing Dart code and creating new source files.

Start by adding these to your pubspec.yaml in a new package intended for your generator (it’s best practice to keep generators separate from your main app).

# In your generator package's pubspec.yaml
dependencies:
  source_gen: ^1.0.0
dev_dependencies:
  build_runner: ^2.0.0

Building a Simple Annotation-Driven Generator

Let’s create a generator that automatically generates a copyWith method for simple data classes. First, we define an annotation to mark which classes should be processed.

Step 1: Define the Annotation

// lib/annotations.dart
class GenerateCopyWith {
  const GenerateCopyWith();
}

Step 2: Create the Generator Now, we write the generator itself. It will look for classes annotated with @GenerateCopyWith() and produce a new .g.dart file.

// lib/builder.dart
import 'package:source_gen/source_gen.dart';
import 'package:build/build.dart';

class CopyWithGenerator extends GeneratorForAnnotation<GenerateCopyWith> {
  @override
  String generateForAnnotatedElement(
    Element element,
    ConstantReader annotation,
    BuildStep buildStep,
  ) {
    // Ensure we're processing a class
    if (element is! ClassElement) {
      throw InvalidGenerationSourceError(
        'The @GenerateCopyWith annotation can only be applied to classes.',
        element: element,
      );
    }

    final className = element.name;
    final fields = element.fields.where((f) => !f.isStatic);

    // Build the parameter list for the copyWith method
    final params = fields.map((f) => '${f.type}? ${f.name}').join(', ');

    // Build the field assignments inside the copyWith method
    final assignments = fields
        .map((f) => '${f.name}: ${f.name} ?? this.${f.name},')
        .join('\n        ');

    return '''
// **************************************************************************
// CopyWithGenerator
// **************************************************************************

extension \$${className}CopyWith on $className {
  $className copyWith({$params}) {
    return $className(
      $assignments
    );
  }
}
''';
  }
}

Step 3: Register the Builder We need to hook our generator into the build system.

// lib/builder.dart (continued)
Builder copyWithBuilder(BuilderOptions options) {
  return LibraryBuilder(
    CopyWithGenerator(),
    generatedExtension: '.copywith.g.dart',
  );
}

Step 4: Use It! Finally, in a consuming Flutter project, you add your generator package and annotate a class.

// lib/user.dart
import 'package:my_generator/annotations.dart';

@GenerateCopyWith()
class User {
  final String name;
  final int id;

  User({required this.name, required this.id});
}

Run dart run build_runner build, and you’ll get a new user.copywith.g.dart file:

// user.copywith.g.dart
// GENERATED CODE - DO NOT MODIFY BY HAND

extension $UserCopyWith on User {
  User copyWith({String? name, int? id}) {
    return User(
      name: name ?? this.name,
      id: id ?? this.id,
    );
  }
}

Best Practices and Lessons Learned

  1. Idempotence is Key: Your generator’s output must be stable. Running it twice on the same source should produce identical output. Use dart_style to format generated code, ensuring consistency.

  2. Handle Errors Gracefully: Validate the annotated elements early. If someone puts @GenerateCopyWith() on a function, provide a clear, actionable error message using InvalidGenerationSourceError.

  3. Make Generated Code Readable: Include clear headers and comments (like // GENERATED CODE) to prevent developers from editing it manually. All manual changes will be overwritten on the next build.

  4. Separate Annotations and Generators: Keep your annotation classes in a separate, lightweight package. This allows your main app to depend only on the annotations, not the heavier generator tooling, improving compile times.

  5. Test Your Generator: Write unit tests for your generator’s logic, feeding it mock Element objects. Also, write integration tests using build_runner to ensure the full pipeline works correctly.

The Payoff

While setting up code generation requires initial effort, the long-term benefits are substantial. You eliminate whole categories of typos, enforce architectural patterns automatically, and make refactoring far less painful. By mastering these tools, you move from writing repetitive code to engineering systems that write it for you.

Start small, perhaps by automating the generation of toString() methods or simple mappers. You’ll quickly see how this powerful technique can clean up your Dart and Flutter projects.

This blog is produced with the assistance of AI by a human editor. Learn more

Related Posts

Cover image for Choosing the Right Storage for Flutter: Beyond SharedPreferences for Persistent Data

Choosing the Right Storage for Flutter: Beyond SharedPreferences for Persistent Data

Developers frequently struggle with persistent storage in Flutter, especially when dealing with OS-level backups and app reinstalls. This post will clarify the nuances of different storage solutions like `SharedPreferences`, `path_provider`, and `flutter_secure_storage`, focusing on how to correctly manage data lifetimes (per-install vs. per-user) and avoid issues like device IDs migrating to new phones or data being unexpectedly backed up.

Cover image for Optimizing Flutter UI Performance: Solving `ImageFiltered` Lag and Smooth Blurring

Optimizing Flutter UI Performance: Solving `ImageFiltered` Lag and Smooth Blurring

Flutter's `ImageFiltered` can cause significant lag when blurring large or frequently changing content like text. This post will explain the performance bottlenecks, provide solutions like caching blurred outputs with `RepaintBoundary` or `SnapshotWidget`, and offer practical tips to ensure smooth UI effects without sacrificing frame rates.