Syntax Declaration
An extension type is declared using theextension type keywords, followed by the name, the primary constructor defining the representation object, and the body.
Representation and Type Erasure
The core characteristic of an extension type is type erasure. While the compiler treats the extension typeE and the representation type int as distinct, they are the same object at runtime.
- Static Analysis:
Eis not a subtype ofint(unless specified viaimplements). Anintcannot be assigned directly to a variable of typeEwithout using the constructor. - Runtime: The type
Eis erased. The runtime type of an instance ofEisint.
Member Access and Encapsulation
Extension types restrict access to the members of the representation type but expose the representation object itself via a getter derived from the primary constructor.- Representation Object Access: The primary constructor parameter (e.g.,
id) automatically induces a getter with the same name on the extension type. To fully encapsulate the representation object, the parameter name must be private (prefixed with_). - Underlying Member Hiding: Methods and properties of the representation type (e.g.,
int.isEven) are not exposed unless explicitly redeclared or exposed viaimplements.
Type Hierarchy and implements
Extension types support a form of inheritance using the implements keyword. This clause allows the extension type to be a subtype of other types, exposing their members.
Implementing Non-Extension Types
An extension type can implement its representation type or any supertype of its representation type. This makes the extension type a subtype of the implemented type and exposes its members.Implementing Other Extension Types
An extension typeSub can implement another extension type Super if the representation type of Sub is a subtype (or the same type) of the representation type of Super.
Constructors
Extension types allow both primary and named constructors.- Primary Constructor: Declared immediately after the type name. It must accept exactly one positional parameter (the representation object).
- Named/Factory Constructors: Can be defined in the body to provide alternative initialization logic.
Member Resolution and Object Members
Extension types have specific rules regarding member resolution and the coreObject members.
Shadowing Implemented Members
If an extension type usesimplements to expose the representation type’s interface, it can still shadow specific members by redeclaring them in the extension type body. The extension type’s declaration takes precedence statically.
Restriction on Object Members
Extension types cannot declare members with the same names as the members ofObject: toString, hashCode, runtimeType, and noSuchMethod.
Because extension types are erased at runtime, calls to these methods always resolve to the underlying representation object’s implementation. This ensures that runtime behavior (like equality checks and string representation) remains consistent with the actual runtime type.
Master Dart with Deep Grasping Methodology!Learn More





