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 enumerated type (enum) in Dart is a special class representing a fixed, predefined set of constant values. All enums automatically extend the core Enum class. They are closed types, meaning they cannot be subclassed, implemented by other classes, or explicitly instantiated at runtime.

Basic Syntax

A simple enum is declared using the enum keyword followed by a comma-separated list of identifiers.
enum ConnectionState {
  disconnected,
  connecting,
  connected,
  disconnecting
}

Intrinsic Properties

Every enum declaration implicitly generates several properties and methods:
  • index: A zero-based integer representing the value’s position in the declaration.
  • name: A string containing the exact identifier name of the enum value.
  • values: A constant List containing all instances of the enum in declaration order.
int position = ConnectionState.connecting.index; // 1
String identifier = ConnectionState.connecting.name; // "connecting"
List<ConnectionState> states = ConnectionState.values;

Enhanced Enums

As of Dart 2.17, enums support class-like features. They can declare state, constructors, methods, and getters. To define an enhanced enum, the following technical constraints apply:
  1. Instance variables must be final.
  2. All generative constructors must be const.
  3. Factory constructors can only return one of the predefined, fixed enum instances.
  4. The list of enum instances must be declared first, and if the enum contains members, the list must terminate with a semicolon (;).
enum HttpStatusCode {
  ok(200, 'Success'),
  badRequest(400, 'Bad Request'),
  internalServerError(500, 'Server Error');

  // Final instance variables
  final int code;
  final String description;

  // Constant generative constructor
  const HttpStatusCode(this.code, this.description);

  // Getter
  bool get isError => code >= 400;

  // Method
  void printStatus() => print('$code: $description');
}

Mixins and Interfaces

While enums cannot extend other classes (as they already extend Enum), they can use mixins (with) and implement interfaces (implements).
mixin Loggable {
  void log() => print('Logging: $hashCode');
}

abstract interface class Serializable {
  String serialize();
}

enum Role with Loggable implements Serializable {
  admin,
  guest;

  @override
  String serialize() => '{"role": "$name"}';
}

Technical Constraints

When working with Dart enums, the compiler enforces strict limitations to guarantee their constant, finite nature:
  • You cannot use the new or const keywords to instantiate an enum outside of its internal declaration.
  • You cannot override the index, hashCode, or the equality operator (==).
  • The values property is reserved; you cannot declare a member named values within an enum.
Master Dart with Deep Grasping Methodology!Learn More