Build Status pub package

Functional Enum

Freezed, but for enums.

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


Add the following dependencies to your project.



With pure dart

import 'package:functional_enum_annotation/functional_enum_annotation.dart';

// assuming your file name is main.dart
part 'main.g.dart';

enum Shape { square, circle, triangle }

void main() {
  final shape =;

  // 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 ==
  print(shape.isCircle); // true
  print(shape.isSquare); // false
  print(shape.isTriangle); // false

With flutter

import 'package:functional_enum_annotation/functional_enum_annotation.dart';

// assuming your file name is main.dart
part 'main.g.dart';

enum AppState { initial, loading, loaded }

class MyWidget extends StatelessWidget {
  final Appstate appState;

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

  Widget build(BuildContext context) {
    return appState.when(
      initial: () => SizedBox(),
      loading: () => CircularProgressIndicator(),
      loaded: () => HomePage(),

Generate the code

Once you have completed the above steps, you should run the build_runner to generate the code for you. You can use the following commands to run the code generator:

  • For dart projects use
$ dart pub run build_runner build
  • and for flutter projects use
$ flutter pub run build_runner build