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.

An enum constructor in Dart allows enumerated types to declare internal state and associate specific values with each enum constant. Because enum instances are inherently compile-time constants, all generative enum constructors must be declared as const, and all instance variables must be final.

Syntax and Structure

To define a constructor within an enum, the enum constants must be declared first, followed by a mandatory semicolon (;). The fields, constructors, and methods are declared after this semicolon.
enum StatusCode {
  success(200, 'OK'),
  badRequest(400, 'Bad Request'),
  internalError(500, 'Internal Server Error'); // Semicolon is required here

  final int code;
  final String message;

  // Generative constructor must be const
  const StatusCode(this.code, this.message);
}

Technical Constraints

  1. Instantiation: Enum constructors cannot be invoked externally. You cannot use new or const to create new instances of an enum outside of its internal declaration block.
  2. Immutability: All instance variables declared within the enum must be final.
  3. Const Requirement: All generative constructors must be prefixed with the const keyword.
  4. Ordering: The list of enum values must be the very first declaration inside the enum body.

Named and Factory Constructors

Dart enums support named constructors, initializer lists, and factory constructors, provided they adhere to the strict immutability and instantiation rules of the enum type.
enum Vehicle {
  car.fourWheels('Sedan'),
  motorcycle.twoWheels('Cruiser'),
  tricycle.custom(wheels: 3, type: 'Trike');

  final int wheels;
  final String type;

  // Named constructor with an initializer list
  const Vehicle.fourWheels(this.type) : wheels = 4;

  // Another named constructor
  const Vehicle.twoWheels(this.type) : wheels = 2;

  // Named constructor with named parameters
  const Vehicle.custom({required this.wheels, required this.type});

  // Factory constructors are permitted but must return an existing enum instance.
  // They cannot instantiate new enum values.
  factory Vehicle.fromWheels(int wheels) {
    return Vehicle.values.firstWhere(
      (v) => v.wheels == wheels,
      orElse: () => Vehicle.car,
    );
  }
}

Mixins and Interfaces

Enum constructors can initialize state required by implemented interfaces or applied mixins. When an enum implements an interface, the enum constructor is responsible for populating the final fields that satisfy the interface contract.
abstract class Describable {
  String get description;
}

enum Status implements Describable {
  active('The item is currently active.'),
  inactive('The item is disabled.');

  // Satisfies the Describable interface
  @override
  final String description;

  const Status(this.description);
}
Master Dart with Deep Grasping Methodology!Learn More