Skip to main content
A named constructor is an explicitly identified constructor that allows a class to define multiple instantiation paths. Because Dart does not support constructor overloading by parameter signature, named constructors provide the mechanism to declare multiple constructors within a single class while explicitly defining the initialization behavior.

Syntax

A named constructor is defined by appending a dot (.) and an identifier to the class name.
class ClassName {
  // Unnamed constructor
  ClassName() { 
    // ... 
  }

  // Named constructor
  ClassName.identifier(parameters) {
    // Initialization logic
  }

  // Private named constructor
  ClassName._privateIdentifier(parameters) {
    // Initialization logic
  }
}

Technical Characteristics

1. No Inheritance Named constructors are not inherited by subclasses. If a subclass needs to be instantiated using a named constructor defined in its superclass, the subclass must explicitly declare its own constructor (named or unnamed) and invoke the superclass’s named constructor via the initializer list. 2. Constructor Redirection A named constructor can delegate initialization to another constructor within the same class using the this keyword in the initializer list. When redirecting, the constructor body must remain empty. 3. Superclass Invocation A named constructor can invoke a specific named constructor of its superclass using the super.identifier() syntax within the initializer list. This invocation must occur before any local constructor body executes. 4. Factory Modifiers Named constructors can be declared with the factory keyword. A factory named constructor does not strictly allocate a new instance; it can return an existing instance from a cache or return an instance of a subtype. 5. Private Named Constructors By prefixing the named constructor’s identifier with an underscore (_), the constructor becomes private to its declaring library. This is a structural pattern used to restrict external instantiation, force the use of factory constructors, or implement singletons.

Implementation Mechanics

The following code demonstrates the structural mechanics of named constructors, including standard initialization, redirection, superclass delegation, and private access control.
class Point {
  double x;
  double y;

  // Standard generative unnamed constructor
  Point(this.x, this.y);

  // Named constructor using an initializer list
  Point.origin() 
      : x = 0.0, 
        y = 0.0;

  // Named constructor redirecting to the standard constructor
  Point.alongXAxis(double x) : this(x, 0.0);

  // Private named constructor restricting external access
  Point._internal(this.x, this.y);
}

class Point3D extends Point {
  double z;

  // Subclass named constructor delegating to a superclass named constructor
  Point3D.origin() 
      : z = 0.0, 
        super.origin();
}

void main() {
  // Instantiation via named constructors
  final p1 = Point.origin();
  final p2 = Point.alongXAxis(5.0);
  final p3 = Point3D.origin();
  
  // Point._internal(1.0, 1.0); // Compilation error if called outside the library
}
Tired of Poor Dart Skills? Fix That With Deep Grasping!Learn More