Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

Enhanced enums in Dart are enumerations augmented with class-like capabilities, allowing them to declare state (fields), behavior (methods), and constructors, as well as implement interfaces and apply mixins. They retain the exhaustive evaluation properties of standard enums while functioning as immutable, compile-time constant objects.

Syntax and Structure

To define an enhanced enum, use the standard enum keyword but expand the body to include class members.
abstract class Describable {
  String get description;
}

mixin Loggable {
  // Mixins applied to enums cannot have instance variables.
  void log() => print(this.toString());
}

enum Status with Loggable implements Describable {
  // 1. Instances MUST be declared first, terminated by a semicolon.
  pending(code: 100, label: 'Awaiting Action'),
  active(code: 200, label: 'In Progress'),
  resolved(code: 300, label: 'Completed');

  // 2. Instance variables must be final.
  final int code;
  final String label;

  // 3. Generative constructors must be const.
  const Status({
    required this.code,
    required this.label,
  });

  // 4. Factory constructors can be used to return existing instances.
  factory Status.fromCode(int code) {
    return Status.values.firstWhere(
      (status) => status.code == code,
      orElse: () => Status.pending,
    );
  }

  // 5. Methods and getters can be defined or overridden.
  @override
  String get description => 'Status $code: $label';

  bool get isTerminal => this == Status.resolved;
}

Technical Constraints and Rules

When constructing enhanced enums, the Dart compiler enforces strict architectural rules to guarantee they remain valid compile-time constants:
  • Instance Declaration Order: The enum instances must be the very first declarations within the enum body. If the enum contains any fields, methods, or constructors, the list of instances must be explicitly terminated with a semicolon (;).
  • Constructor Restrictions: All generative constructors must be declared as const. You cannot instantiate new enum values at runtime. Factory constructors are permitted, but they must return one of the statically defined enum instances.
  • Immutability: Because enum instances are compile-time constants, all instance variables declared within an enhanced enum must be final.
  • Inheritance Limitations: All enums implicitly extend the base Enum class. Consequently, an enhanced enum cannot use the extends keyword. Additionally, an enum cannot be extended, implemented, or mixed into any other class or enum.
  • Interfaces and Mixins: You can use the implements clause to fulfill interface contracts and the with clause to apply mixins. However, any applied mixin must not declare any instance variables (neither mutable nor final) and must not declare generative constructors. This is required because applying a mixin with instance variables prevents the resulting enum from maintaining the required const generative constructors.
  • Equality and Hashing: Enhanced enums are strictly prohibited from overriding the == operator and hashCode. This is a fundamental rule enforced by the compiler to guarantee the integrity of enum instances as unique, compile-time constants.
  • Reserved Identifiers: Enum instances cannot be named values, name, or index. These are reserved properties automatically generated by the Dart compiler for the underlying Enum implementation.
Master Dart with Deep Grasping Methodology!Learn More