Sub topic 8
Redirecting constructors in Dart
Prerequisites and the working model
Assume you know basic Dart classes, named constructors, final fields, and null safety.
A redirecting constructor does not create an object itself. It forwards construction to another constructor in the same class. The target constructor performs the initialization, so the redirecting constructor cannot have a body.
Use a colon followed by this(...) to redirect to another constructor. Arguments passed to this(...) can be expressions based on the original arguments.
import 'dart:math' as math;
class Point {
final double x;
final double y;
const Point(this.x, this.y);
// Redirects to Point(double, double).
Point.fromPolar(double radius, double angle)
: this(
radius * math.cos(angle),
radius * math.sin(angle),
);
// A const redirecting constructor.
const Point.origin() : this(0, 0);
@override
String toString() => 'Point($x, $y)';
}
void main() {
final point = Point.fromPolar(10, math.pi / 2);
const origin = Point.origin();
print(point);
print(origin);
}Point.fromPolar translates polar coordinates into Cartesian coordinates, then redirects to the unnamed constructor. The unnamed constructor initializes x and y. Point.origin redirects with constant values and can therefore be declared const.
A redirecting constructor must not contain an initializer list that initializes fields separately, and it must not contain a constructor body. The target constructor owns the complete initialization process.
Redirecting named constructors
Redirecting constructors are useful when several public construction forms should share one implementation. This prevents duplicated validation and field assignments.
For example, a class can expose constructors for different input formats while keeping one canonical constructor responsible for storing values:
| Constructor | Input form | Target |
|---|---|---|
| `Point(x, y)` | Cartesian coordinates | Creates the object |
| `Point.fromPolar(radius, angle)` | Polar coordinates | `Point(x, y)` |
| `Point.origin()` | No coordinates | `Point(0, 0)` |
The target must be reachable from the redirecting constructor. If the target requires arguments, the redirecting constructor must provide them. It cannot redirect to an arbitrary constructor in another class using this; use a factory redirect or regular construction for that case.
Common errors
- Adding a body after a redirect. A redirecting constructor must end after its target invocation.
- Trying to initialize a
finalfield in both constructors. The target constructor must do the initialization. - Forgetting that redirecting constructors still obey
construles. Every expression used by a const construction path must be compile-time constant. - Choosing a redirect when the constructor needs unique behavior after construction. Use a regular constructor when it needs a body or its own initialization steps.
Exercise
Why can const Point.origin() : this(0, 0); be constant, while a redirecting constructor that calls math.cos(angle) cannot generally be constant?
Show answer
The origin constructor uses compile-time constant values. math.cos(angle) is a runtime calculation, so it cannot be used in a const construction path.