Skip to main content
A named constructor is a constructor declaration that appends a specific identifier to the class name (ClassName.identifier). This mechanism allows a class to define multiple distinct constructors, distinguishing them by name rather than relying solely on parameter signatures.

Syntax

The syntax consists of the class name, a dot, and a unique identifier.
class ClassName {
  // Standard (unnamed) constructor
  ClassName(int x);

  // Named constructor declaration
  ClassName.identifier(int x, int y) {
    // Initialization logic
  }
}

// Instantiation
var instance = ClassName.identifier(10, 20);

Classification

Named constructors are categorized as either generative or factory constructors.

1. Named Generative Constructors

A named generative constructor creates a new instance of the class. It initializes instance variables and executes an optional body. Initialization Rules Object creation occurs in two phases:
  1. Initialization Phase: All non-nullable, non-late instance variables must be initialized. This is achieved via initializing formals (e.g., this.field) or the initializer list.
  2. Body Execution Phase: The constructor body executes after the instance is fully initialized. Assignments within the body are valid for any non-final field, provided the field was correctly initialized during the first phase.
class Vector {
  double x;
  double y;

  // Named constructor using an initializer list
  // Required for non-nullable fields not using 'this.' syntax
  Vector.origin()
      : x = 0,
        y = 0;
  
  // Named constructor using initializing formals
  Vector.explicit(this.x, this.y);

  // Named constructor with a body
  Vector.adjusted(double start)
      : x = start,
        y = start {
    // Valid: Reassigning non-final fields after initialization
    x += 10; 
  }
}

2. Named Factory Constructors

A named constructor prefixed with the factory keyword does not implicitly create a new instance of the class. Instead, it must explicitly return an instance of the class or a subtype. Factory constructors do not have access to this and cannot use initializer lists to set instance fields directly. A factory constructor must return a non-nullable instance; returning null results in a compile-time error.
class Logger {
  static final Map<String, Logger> _cache = {};

  // Named factory constructor
  factory Logger.fromName(String name) {
    return _cache.putIfAbsent(name, () => Logger._internal(name));
  }

  // Private named generative constructor used by the factory
  Logger._internal(String name);
}

Delegation and Chaining

Redirecting Constructors

A named constructor can delegate initialization to another constructor within the same class using the this keyword. A redirecting constructor cannot have a body.
class Point {
  double x, y;

  Point(this.x, this.y);

  // Redirects to the unnamed generative constructor
  Point.alongXAxis(double x) : this(x, 0);
}

Superclass Initialization

If a class inherits from a superclass, a named generative constructor must ensure the superclass is initialized. If the superclass does not provide a default (unnamed, no-argument) constructor, the subclass must explicitly invoke an available superclass constructor via the super keyword in the initializer list.
class Parent {
  Parent.fromJson(Map<String, dynamic> data);
}

class Child extends Parent {
  // Explicitly chains to the named constructor of the superclass
  Child.fromJson(Map<String, dynamic> data) : super.fromJson(data);
}

Scope and Visibility

Named constructors adhere to library privacy rules based on the identifier used:
  • Public: ClassName.name is accessible from any library.
  • Private: ClassName._name is accessible only within the library where the class is defined.

Inheritance Limitations

Constructors are not inherited. A subclass does not automatically acquire the named constructors of its superclass. To expose a named constructor in a subclass that matches the signature of a superclass constructor, the subclass must explicitly declare it and chain it to the superclass.
Master Dart with Deep Grasping Methodology!Learn More