> ## 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.

# Dart Enum Constructor

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.

```dart theme={"dark"}
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.

```dart theme={"dark"}
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.

```dart theme={"dark"}
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);
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Dart Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
