DartPoet Docs Help

Enum

Enumerated types, often called as enums, are a special kind of class used to represent a fixed number of constant values.

Declaring simple enums

DartPoet handles the creation from enums over the ClassSpec with a specific builder method.

val enumClass = ClassSpec.builder("Color") .enumProperties( EnumPropertySpec.builder("red").build(), EnumPropertySpec.builder("green").build(), EnumPropertySpec.builder("blue").build(), ) .build()

Results in the following code:

enum Color { red, green, blue }

Declaring enhanced enums

The programming languages Dart also allow the declaration from enhanced enums. It can contain fields, methods and const constructors which are limited to a fixed number of known constant instances.

But the creation of enhanced enums contains some special requirements that are important to know:

  1. Instance variables must be final, including those added by mixins.

  2. All generative constructors must be constant.

  3. Factory constructors can only return one of the fixed, known enum instances.

  4. No other class can be extended as Enum is automatically extended.

  5. There cannot be overrides for index, hashCode, the equality operator ==.

  6. A member named values cannot be declared in an enum, as it would conflict with the automatically generated static values' getter.

  7. All instances of the enum must be declared in the beginning of the declaration, and there must be at least one instance declared.

Now let's create an enhanced enum. The involved code for this is similar to the creation of a simple enum, but with some additional function calls: We reuse the code from the simple enum and add the required functions call to retrieve the enhanced enum.

val enumClass = ClassSpec.builder("Color") .enumProperties( EnumPropertySpec.builder("red").parameter("%C", "red").build(), EnumPropertySpec.builder("green").parameter("%C", "green").build(), EnumPropertySpec.builder("blue").parameter("%C", "blue").build(), ) .parameters( ParameterSpec.builder("color", String::class).build() ) .constructor( ConstructorSpec.builder() .parameters( ParameterSpec.builder("color").build() ) .build() ) .build()

When you add the parameter entries to an EnumProperty, you can do it over two different ways. The first is you use a CodeBlock which contains the necessary content or use the parameter method which takes a specific format string. For more information about the allowed formatting, take a quick look at the Placeholders page.

Which results in the following code:

enum Color { red('red'), green('green'), blue('blue'); final String color; const Color(this.color); }
Last modified: 11 March 2024