enum_class 1.1.2 enum_class: ^1.1.2 copied to clipboard
Dart classes as enums. This library is the runtime dependency.
Enum Classes for Dart #
Introduction #
Enum Classes provide classes with enum features and are part of Libraries for Object Oriented Dart.
Motivation #
Enums are very helpful in modelling the real world: whenever there are a small fixed set of options, an enum is a natural choice. For an object oriented design, though, enums need to be classes. Dart falls short here, so Enum Classes provide what's missing!
Design #
- Constants have
name
andtoString
, can be used inswitch
statements, and are real classes that can hold code and implement interfaces - Generated
values
method that returns all the enum values in aBuiltSet
(immutable set) - Generated
valueOf
method that takes aString
Using Enum Classes #
Enum Classes use the source_gen
library for code generation. The typical way to use it is via a build.dart
tool that you create for your project. When you run it, all the generated files
are updated.
Here's what you need to do to use Enum Classes:
- Add a library dependency on enum_class to your pubspec.yaml
- Add a dev dependency on enum_class_generator to your pubspec.yaml
- Create a
build.dart
for your project. See example, below. - Run
pub run tools/build.dart
whenever you need to update the generated files. - To make an Enum Class, import
package:enum_class/enum_class.dart
then extend EnumClass.
See
this example
for a full project with a build.dart
and an enum.
Features and bugs #
Please file feature requests and bugs at the issue tracker.