Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/stevenroose/dart-enums

A library to create Enums in Dart.
https://github.com/stevenroose/dart-enums

Last synced: about 2 months ago
JSON representation

A library to create Enums in Dart.

Awesome Lists containing this project

README

        

dart-enums
==========
[![Build Status](https://drone.io/github.com/stevenroose/dart-enums/status.png)](https://drone.io/github.com/stevenroose/dart-enums/latest)

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:

```dart
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 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
}
```