Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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.
- Host: GitHub
- URL: https://github.com/stevenroose/dart-enums
- Owner: stevenroose
- License: mit
- Created: 2014-09-30T16:16:39.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2014-12-01T20:24:49.000Z (about 10 years ago)
- Last Synced: 2024-10-30T20:24:15.144Z (3 months ago)
- Language: Dart
- Size: 196 KB
- Stars: 1
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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
}
```