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 method in Dart is a function defined within an enumerated type (enum) that encapsulates logic specific to the enum’s constant values. Since Dart 2.17, enhanced enums support instance methods, static methods, getters, and method overrides, allowing enums to behave similarly to standard classes while maintaining a fixed, immutable number of constant instances.

Syntax and Structural Rules

To declare methods within an enum, the following structural constraints must be met:
  1. Value Declaration First: The constant enum values must be declared at the very beginning of the enum body.
  2. Trailing Semicolon: The list of enum values must be terminated with a semicolon ; before declaring any fields, constructors, or methods.
  3. Strict Immutability: Because enum instances are inherently compile-time constants, all instance fields must be declared as final, and constructors must be const. Consequently, methods and setters cannot mutate the state of an enum instance.
  4. Restricted Overrides: Enum methods cannot override ==, hashCode, or index, as these are strictly managed by the Dart runtime for enum types.

Code Visualization

enum HttpMethod {
  get('GET', true),
  post('POST', false),
  put('PUT', false),
  delete('DELETE', false); // Mandatory semicolon terminates the value list

  // All instance fields must be final because enum instances are compile-time constants
  final String value;
  final bool isIdempotent;

  // Constant constructor
  const HttpMethod(this.value, this.isIdempotent);

  // 1. Instance Method
  // Operates on the specific enum instance without mutating state
  bool requiresBody() {
    return this == HttpMethod.post || this == HttpMethod.put;
  }

  // 2. Getter
  // Provides computed properties based on the instance state
  String get formattedName => 'Method: $value';

  // 3. Static Method
  // Operates on the enum type itself
  static HttpMethod parse(String input) {
    return HttpMethod.values.firstWhere(
      (method) => method.value == input.toUpperCase(),
      orElse: () => throw ArgumentError('Invalid HTTP method'),
    );
  }

  // 4. Method Override
  // Customizes standard Object methods (except == and hashCode)
  @override
  String toString() => 'HttpMethod.$name ($value)';
}

Method Invocation

Enum methods are invoked using standard dot notation, identical to class method invocation. Instance methods are called on the specific enum value, while static methods are called on the enum type itself.
// Invoking an instance method
bool hasBody = HttpMethod.post.requiresBody(); 

// Invoking a getter
String name = HttpMethod.get.formattedName; 

// Invoking a static method
HttpMethod parsedMethod = HttpMethod.parse('put'); 

// Invoking an overridden method implicitly
print(HttpMethod.delete); 
Master Dart with Deep Grasping Methodology!Learn More