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 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.
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.
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.
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.
HttpResponse parsedValue = HttpResponse.values.byName('unauthorized');
print(parsedValue.code); // 401

// Throws ArgumentError: Invalid value: Not found: "serverError"
// HttpResponse.values.byName('serverError');
Master Dart with Deep Grasping Methodology!Learn More