dart-enums

Build Status

Dart does not have native support for Enums and this library tries to fill this void. Largely inspired by Java's enum features, this library provides an extendable Enum class that provides all enum instances with an ordinal and a name. Also a valueOf constructor and values() iterable are provided.

Example use:

class MyEnum extends Enum {
  // the enum values
  static const MyEnum nr1 = const MyEnum._(5);
  static const MyEnum nr2 = const MyEnum._(10);
  static const MyEnum nr3 = const MyEnum._(15);

  // these two lines are required to add support for values and valueOf
  static MyEnum valueOf(String name) => Enum.valueOf(MyEnum, name);
  static List<MyEnum> get values => Enum.values(MyEnum);

  // your own implementation
  final int myValue;
  const MyEnum._(this.myValue);
}

void main() {
  print(MyEnum.nr1.index);            // prints 0
  print(MyEnum.nr1.toString());       // prints nr1
  print(MyEnum.valueOf("nr1").index); // prints 0
  print(MyEnum.values[1].toString())  // prints nr2
  print(MyEnum.values.last.index)     // prints 2
  print(MyEnum.values.last.myValue);  // prints 15
}  

Libraries

enums