Enum Classes are an extension to Dart that uses code generation to give common enum functionality to classes.
- Constants have
nameandtoString, can be used inswitchstatements, and are real classes that you can add code to - Generated
valuesmethod that returns all the enum values in aBuiltSet(immutable set) - Generated
valueOfmethod that takes aString
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.dartfor your project. See example, below. - Run
pub run tools/build.dartwhenever you need to update the generated files. - To make an Enum Class, import
package:enum_class/enum_class.dartthen extend EnumClass.
See
this example
for a full project with a build.dart and an enum.
Please file feature requests and bugs at the issue tracker.