enum_generator 1.0.7 copy "enum_generator: ^1.0.7" to clipboard
enum_generator: ^1.0.7 copied to clipboard

outdated

This is a lightweight library to create functionality for enum class type and make enum usage much better.

Generating the functional Enum #

Freezed, but for enums.

This is a lightweight library to create functionality for enum class type and make enum usage much better.

Installation #

Add the following dependencies to your project.


dependencies:
  enum_annotation: ^0.0.2

dev_dependencies:
  build_runner: any
  enum_generator: any

Usage #

Now generate the code using build_runner.

Dart only:

dart pub run build_runner build
// OR
dart pub run build_runner watch

Flutter:

flutter pub run build_runner build
// OR
flutter pub run build_runner watch

With pure dart

import 'package:enum_generator/src/annotation.dart';

part 'main.g.dart';

@generate
enum Shape { square, circle, triangle }

void main() {
  final shape = Shape.circle;

  // all cases must be handled
  final message = shape.when(
    square: () => 'I am a Square',
    circle: () => 'I am a Circle',
    triangle: () => 'I am a Triangle',
  );
  print(message); // I am a Circle

  // all cases may not be handled but `orElse` cannot be null
  final canBeRotated = shape.maybeWhen(
    circle: () => false,
    orElse: () => true,
  );
  print('Can be rotate: $canBeRotated'); // false

  // equivalent to print(shape == Shape.circle)
  print('The shape is circle: ${shape.isCircle}'); // true
  print('The shape is Square: ${shape.isSquare}'); // false
  print('The shape is Triangle: ${shape.isTriangle}'); // false
}
2
likes
0
pub points
40%
popularity

Publisher

unverified uploader

This is a lightweight library to create functionality for enum class type and make enum usage much better.

Homepage
Repository (GitHub)
View/report issues

Documentation

Documentation

License

unknown (license)

Dependencies

analyzer, build, build_config, enum_annotation, flutter, source_gen

More

Packages that depend on enum_generator