frx_generator 1.0.0+1 copy "frx_generator: ^1.0.0+1" to clipboard
frx_generator: ^1.0.0+1 copied to clipboard

Code generator for frx_annotation package that generates pattern matching methods for union types in Dart

frx_generator #

Code generator for the frx_annotation package that generates pattern matching methods for union types in Dart.

Features #

  • Generates extension methods for sealed classes
  • Supports pattern matching with when, maybeWhen, and whenOrNull
  • Generates type-safe mapping methods (map, maybeMap, mapOrNull)
  • Full support for Dart 3.0 pattern matching syntax

Installation #

Add these dependencies to your package's pubspec.yaml file:

dependencies:
  frx_annotation: ^1.0.0

dev_dependencies:
  build_runner: ^2.4.0
  frx_generator: ^1.0.0

Usage #

  1. Import and use the @frx annotation from frx_annotation:
import 'package:frx_annotation/frx_annotation.dart';

part 'my_union.frx.g.dart';

@frx
sealed class Result {
  const Result();
}

final class Success extends Result {
  final String value;
  const Success(this.value);
}

final class Error extends Result {
  const Error();
}
  1. Run the generator:
dart run build_runner build

Generated API #

The generator will create extension methods for pattern matching:

// Example usage of generated code
void example(Result result) {
  final message = result.when(
    success: (value) => 'Success: $value',
    error: () => 'Error occurred',
  );
  
  final maybeMessage = result.maybeWhen(
    success: (value) => 'Success: $value',
    orElse: () => 'Other case',
  );
}

Additional Information #

2
likes
0
points
17
downloads

Publisher

unverified uploader

Weekly Downloads

Code generator for frx_annotation package that generates pattern matching methods for union types in Dart

Repository (GitHub)
View/report issues

Topics

#codegen #pattern-matching #union-types #generator

License

unknown (license)

Dependencies

analyzer, build, flutter, frx_annotation, source_gen, source_helper

More

Packages that depend on frx_generator