functional_enum 1.0.3

Dart native

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(),
    );
  }
}
0
likes
80
pub points
16%
popularity

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

Repository (GitHub)
View/report issues

Documentation

API reference

Uploader

eliasandualem8@gmail.com

License

MIT (LICENSE)

Dependencies

analyzer, build, build_config, functional_enum_annotation, meta, source_gen

More

Packages that depend on functional_enum