Python Named, Redirecting & Factory Constructors
Dart supports multiple constructor variations: **Named Constructors** (`User.fromJson()`, `Point.origin()`) provide clear semantic instantiation pathways; **Redirecting Constructors** (`Point.origin() : this(0, 0);`) delegate to another constructor; and **Factory Constructors** (`factory User.cached()`) return existing cached instances or subtypes without allocating a new object.
"A named constructor is like having different specialized entrances to a stadium: the VIP Entrance, Media Gate, and General Admission all lead inside, but each prepares you differently."
Deep Dive: How It Works
Named Constructors: Allows a class to have multiple distinct constructors with descriptive names (e.g. `DateTime.now()`, `DateTime.utc()`).
Redirecting Constructors: Forwards calls to another constructor in the same class using `: this(...)`.
Factory Constructors (`factory`): Does not necessarily create a new instance; can return cached singletons, instantiate child subclasses, or parse JSON payloads.
Syntax Blueprint
class Point {
final double x, y;
Point(this.x, this.y);
// Named constructor
Point.origin() : this(0, 0); // Redirecting
// Factory constructor
factory Point.fromJson(Map<String, double> json) {
return Point(json["x"] ?? 0, json["y"] ?? 0);
}
}Named, redirecting, and factory constructor patterns.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Trying to access `this` inside a `factory` constructor body before returning an instance.Why it happens: A factory constructor has no `this` receiver until it explicitly returns an object.
How to fix: Construct and return the instance: `return MyClass._internal();`.
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Create Point with Named Origin Constructor
Create class `Point` with `final int x, y; Point(this.x, this.y);`.
Add redirecting constructor `Point.origin() : this(0, 0);`.
In `main()`, instantiate `final p = Point.origin();` and print `Point: (${p.x}, ${p.y})`.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.