Enhanced enums in Dart are enumerations augmented with class-like capabilities, allowing them to declare state (fields), behavior (methods), and constructors, as well as implement interfaces and apply mixins. They retain the exhaustive evaluation properties of standard enums while functioning as immutable, compile-time constant objects.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.
Syntax and Structure
To define an enhanced enum, use the standardenum keyword but expand the body to include class members.
Technical Constraints and Rules
When constructing enhanced enums, the Dart compiler enforces strict architectural rules to guarantee they remain valid compile-time constants:- Instance Declaration Order: The enum instances must be the very first declarations within the enum body. If the enum contains any fields, methods, or constructors, the list of instances must be explicitly terminated with a semicolon (
;). - Constructor Restrictions: All generative constructors must be declared as
const. You cannot instantiate new enum values at runtime. Factory constructors are permitted, but they must return one of the statically defined enum instances. - Immutability: Because enum instances are compile-time constants, all instance variables declared within an enhanced enum must be
final. - Inheritance Limitations: All enums implicitly extend the base
Enumclass. Consequently, an enhanced enum cannot use theextendskeyword. Additionally, an enum cannot be extended, implemented, or mixed into any other class or enum. - Interfaces and Mixins: You can use the
implementsclause to fulfill interface contracts and thewithclause to apply mixins. However, any applied mixin must not declare any instance variables (neither mutable norfinal) and must not declare generative constructors. This is required because applying a mixin with instance variables prevents the resulting enum from maintaining the requiredconstgenerative constructors. - Equality and Hashing: Enhanced enums are strictly prohibited from overriding the
==operator andhashCode. This is a fundamental rule enforced by the compiler to guarantee the integrity of enum instances as unique, compile-time constants. - Reserved Identifiers: Enum instances cannot be named
values,name, orindex. These are reserved properties automatically generated by the Dart compiler for the underlyingEnumimplementation.
Master Dart with Deep Grasping Methodology!Learn More





