functional_enum 1.1.0 copy "functional_enum: ^1.1.0" to clipboard
functional_enum: ^1.1.0 copied to clipboard

outdated

Code generator for functional enum that makes the use of enums much better.

Build Status pub package

Functional Enum #

Code generator for functional enum that makes the use of enums much better.

Usage #

import 'package:functional_enum_annotation/functional_enum_annotation.dart';

part 'main.g.dart';

@functional_enum
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(canBeRotated); // false

  // equivalent to print(shape == Shape.circle)
  print(shape.isCircle); // true
  print(shape.isSquare); // false
  print(shape.isTriangle); // false
}

With flutter:

@functional_enum
enum AppState { initial, loading, loaded }

class ExampleWidget extends StatelessWidget {
  final Appstate appState;

  const ExampleWidget({Key key, this.appState}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return appState.when(
      initial: () => SizedBox(),
      loading: () => CircularProgressIndicator(),
      loaded: () => HomePage(),
    );
  }
}
14
likes
0
pub points
33%
popularity

Publisher

unverified uploader

Code generator for functional enum that makes the use of enums much better.

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

analyzer, build, build_config, functional_enum_annotation, meta, source_gen

More

Packages that depend on functional_enum