Provides Dart Build System builder for generating EasyEnum extensions for classes annotated with easy_enum.

Pub Package Build Status


In your pubspec.yaml file:

  • Add to dependencies section easy_enum: ^1.0.0
  • Add to dev_dependencies section easy_enum_generator: ^2.0.1
  • Add to dev_dependencies section build_runner: ^2.1.4
  • Set environment to at least Dart 2.13.0 version like so: ">=2.13.0 <3.0.0"

Your pubspec.yaml should look like so:

name: project_name
description: project description
version: 1.0.0

  sdk: ">=2.13.0 <3.0.0"

  easy_enum: ^1.0.0

  build_runner: ^2.1.4
  easy_enum_generator: ^2.0.1

Annotate your class with EasyEnum annotation:

import 'package:easy_enum/easy_enum.dart';

part 'basic_class.g.dart';

enum SimpleState { play, paused, stopped }

@EasyEnum(toValue: 'theValue', toEnum: 'theRating')
enum SomeRating { none, dislike, like }

class BasicClass {

Make sure that you set the part file as in the example above part 'your_file_name.g.dart';.

Launch code generation:

flutter pub run build_runner build

The extension will be generated:


part of 'basic_class.dart';

// **************************************************************************
// EasyEnumGenerator
// **************************************************************************

extension SimpleStateExt on SimpleState {
  String get value => ['play', 'paused', 'stopped'][index];

extension SimpleStateTxe on String {
  SimpleState? get simpleState => {
        'paused': SimpleState.paused,
        'stopped': SimpleState.stopped,

extension SomeRatingExt on SomeRating {
  String get theValue => ['none', 'dislike', 'like'][index];

extension SomeRatingTxe on String {
  SomeRating? get theRating => {
        'none': SomeRating.none,
        'dislike': SomeRating.dislike,

Now the code that previously would have looked like this:

String getStringFromSimpleState(SimpleSate simple) {
  switch (simple) {
    case return 'play';
    case SimpleSate.paused: return 'paused';
    case SimpleSate.stopped: return 'stopped';
var someVar = getStringFromSimpleState(SimpleSate.paused);


Gets simplified to:


In addtion, you can turn a string into its associated enum:

String someVal = getStatusFromApi();  //returns the string 'paused'

if ( someVal.simpleState == SimpleState.paused ) {

Additional features

Alternate getter names

By default you can get the string representation of your enum using the value getter and get the enum representation to the string by using the camelCase name of the enum, ex. with "enum SimpleState ..." the enum is retrieved with "myString.simpleState".

If you want to change the getter names for retrieving the related enum values you can add parameters to the annotation like this:

@EasyEnum(toValue: 'theValue', toEnum: 'theState')
enum SimpleState { play, paused, stopped }

Now the getter methods have been altered allowing retrieval with:


print('paused'.theState == SimpleState.paused);