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

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

example/lib/main.dart

import 'package:enum_annotation/enum_annotation.dart';

part 'main.g.dart';

@generate
enum Shape {
  square(10, 'square'),
  circle(90, 'circle'),
  triangle(1, 'triangle'),
  hexagon(6, 'hexagon');

  final int data;
  final String message;

  const Shape(this.data, this.message);
}

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',
    hexagon: () => 'I am a hexagon'
  );
  print(message); // I am a Circle

  // all cases may not be handled but `orElse` cannot be null
  final canBeRotated = shape.maybeWhen(
    hexagon: () => true,
    orElse: () => false,
  );
  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
160
pub points
39%
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
API reference

License

Apache-2.0 (license)

Dependencies

analyzer, build, build_config, enum_annotation, flutter, source_gen

More

Packages that depend on enum_generator