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

An enum value in Dart is a compile-time constant instance of an enumerated type (`enum`). Each value represents a distinct, named, and immutable singleton object within the closed set defined by the enum declaration. Because they are singletons, enum values are compared by identity, and the Dart compiler explicitly prohibits overriding their `==` operator or `hashCode`.

Every enum type automatically extends the `Enum` class. Consequently, each enum value possesses two default properties:

* `index`: A zero-based integer representing the value's position in the declaration order.
* `name`: A string representation of the value's exact identifier.

```dart theme={"dark"}
enum ConnectionState {
  disconnected,
  connecting,
  connected
}

ConnectionState state = ConnectionState.connecting;
print(state.index); // 1
print(state.name);  // "connecting"
```

## Enhanced Enum Values

As of Dart 2.17, enum values can encapsulate custom state and behavior. In an enhanced enum, each value declaration acts as an invocation of a constant constructor.

When defining enhanced enum values, the following technical constraints apply:

1. All instance variables must be declared `final`.
2. All generative constructors must be `const`.
3. The list of enum values must be terminated with a semicolon (`;`) before declaring fields, constructors, or methods.
4. The enum type cannot be subclassed, implemented, or explicitly instantiated anywhere using `new` or `const`. The only valid instances are the explicitly declared enum values themselves.

```dart theme={"dark"}
enum HttpResponse {
  ok(200, 'Success'),
  unauthorized(401, 'Unauthorized'),
  notFound(404, 'Not Found'); // Semicolon required here

  final int code;
  final String description;

  // Constant constructor
  const HttpResponse(this.code, this.description);
  
  // Methods can be invoked on individual enum values
  bool get isError => code >= 400;
}

HttpResponse response = HttpResponse.notFound;
print(response.code);        // 404
print(response.description); // "Not Found"
print(response.isError);     // true
```

## The `values` Constant

The Dart compiler automatically generates a static `values` property for every enum type. This property returns an unmodifiable `List` containing all the enum values in the exact order they were declared.

```dart theme={"dark"}
List<HttpResponse> allResponses = HttpResponse.values;
print(allResponses[0] == HttpResponse.ok); // true
```

## Value Resolution by Name

You can resolve a specific enum value from a string using the `.byName()` method provided by the enum's `values` iterable (e.g., `<EnumName>.values.byName()`). This method performs an exact string match against the `name` property of the enum values.

If the provided string does not exactly match any enum value's name, the method throws an `ArgumentError`.

```dart theme={"dark"}
HttpResponse parsedValue = HttpResponse.values.byName('unauthorized');
print(parsedValue.code); // 401

// Throws ArgumentError: Invalid value: Not found: "serverError"
// HttpResponse.values.byName('serverError');
```

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