---
title: "Reusing Construction Logic with Named, Factory, and Redirecting Constructors in Dart"
date: "2026-12-03T10:10:00+07:00"
description: "Named constructors add clarity, redirecting constructors reuse logic, and factory constructors cache or validate. Choose the right one in Dart 3."
author: "AI Assistant"
image: "https://blog.redlinesoft.net/images/post/dart-named-factory-redirecting-constructors.jpg"
alt: "Conceptual illustration of several doorways into the same object blueprint, each labeled for a different construction path"
tags: ['dart', 'dart-language', 'oop', 'constructors', 'factory']
---

One class often needs several honest ways to come into existence — an origin point, a point from JSON, a shared cached instance — and Dart gives you four constructor flavors so each path reads like a sentence instead of a flag-laden factory method.

## Named constructors give intent a name

A constructor name can be `ClassName` or `ClassName.identifier`. Named constructors exist to clarify intent and to let one class offer multiple construction routes, so `Point.origin()` says more at the call site than `Point(0, 0)` ever could:

```dart
class Point {
  final double x;
  final double y;
  const Point(this.x, this.y);
  const Point.origin() : x = 0, y = 0;
}

void main() {
  print(Point(3, 4)); // Instance of 'Point'
  print(Point.origin()); // Instance of 'Point'
}
```

Two caveats travel with named constructors: a subclass does **not** inherit them, so redeclare in the subclass whatever routes a subclass needs; and constructors are never inherited at all — a subclass gets only the default constructor if it declares no constructors of its own.

## Redirecting constructors: one source of truth

When several constructors would do the same work, let them all funnel into one. A redirecting generative constructor has an empty body and delegates with `this` after the colon, which keeps initialization, validation, and defaulting in exactly one place:

```dart
class Point {
  double x;
  double y;
  Point(this.x, this.y);
  Point.alongXAxis(double x) : this(x, 0);
}

void main() {
  final p = Point.alongXAxis(5);
  print('(${p.x}, ${p.y})'); // (5.0, 0.0)
}
```

`Point.alongXAxis` only translates its argument and hands everything else to `Point(...)`. Because a redirecting constructor runs no body of its own, it can even be `const` when its target is constant — `const Point.origin() : this(0, 0);` compiles and canonicalizes like any other constant.

## Factory constructors: when construction is not a simple `new`

Reach for the `factory` keyword in exactly two situations: the constructor does not always create a new instance (it may return an object from a cache, or an instance of a subtype), or you need non-trivial work before an instance can exist — argument checking, parsing, or other processing the initializer list cannot express.

```dart
class Logger {
  final String name;
  bool mute = false;
  static final Map<String, Logger> _cache = <String, Logger>{};
  factory Logger(String name) {
    return _cache.putIfAbsent(name, () => Logger._internal(name));
  }
  factory Logger.fromJson(Map<String, Object> json) {
    return Logger(json['name'].toString());
  }
  Logger._internal(this.name);
  void log(String msg) {
    if (!mute) print('$name: $msg');
  }
}

void main() {
  final a = Logger('UI');
  print(identical(a, Logger('UI'))); // true — both from the cache
  Logger.fromJson({'name': 'net'}).log('connected'); // net: connected
}
```

Notice the shape: the public factory routes to a private generative constructor (`Logger._internal`) that does the real initialization, and `fromJson` reuses the caching factory rather than duplicating its logic. The hard rule to remember: **factory constructors cannot access `this`** — there is no instance yet, and there may never be a fresh one.

## Redirecting factory constructors

A redirecting factory skips the body entirely and points at another class's constructor with `=`:

```dart
abstract class Transformer {
  const factory Transformer.upper() = UpperTransformer;
  String apply(String input);
}

class UpperTransformer implements Transformer {
  const UpperTransformer();
  @override
  String apply(String input) => input.toUpperCase();
}

void main() {
  const t = Transformer.upper();
  print(t.apply('dart')); // DART
}
```

Ordinary factories can already return other classes, so redirecting factories look redundant — but they buy two things. First, an abstract class can expose a *constant* constructor that redirects to another class's constant constructor, exactly as `Transformer.upper()` does. Second, the forwarder inherits the target's formal parameters and default values instead of repeating them, so a signature change lands in one place instead of three.

## Constructor tear-offs

You can hand a constructor to code that expects a function by referencing it without parentheses — a tear-off, just like the method tear-offs in `dart-closures-function-tear-offs.md`:

```dart
void main() {
  final charCodes = [68, 97, 114, 116];
  final strings = charCodes.map(String.fromCharCode).toList();
  final buffers = charCodes.map(StringBuffer.new).toList();
  print(strings.join(' ')); // Dart
  print(buffers.first.toString()); // 68
}
```

`String.fromCharCode` is a named constructor; `StringBuffer.new` is the unnamed one. Both are the constructor itself rather than a lambda wrapping it — the `unnecessary_lambdas` lint nudges you if you write `(code) => String.fromCharCode(code)`.

## Which constructor should you reach for?

| Construction need | Tool | Shape |
| --- | --- | --- |
| One obvious way to build the object | Unnamed generative | `Point(3, 4)` |
| Several meaningful, named routes | Named constructor | `Point.origin()` |
| Several routes sharing one implementation | Redirecting generative | `Point.alongXAxis(x) : this(x, 0)` |
| Cache, subtype, or pre-compute before building | Factory | `factory Logger(name)` |
| Expose another class's constructor through an abstraction | Redirecting factory | `factory Transformer.upper() = UpperTransformer` |
| All fields `final`, nothing but assignment | Constant constructor | `const ImmutablePoint(0, 0)` |

If you find yourself writing a boolean parameter like `Point(x, y, isOrigin: true)` to select behavior, a named constructor is the fix. If the boolean selects *which class* to build, or needs real computation first, make it a factory.

## How the syntax keeps evolving

The traditional form repeats the class name — `Point.origin()`, `factory Logger(name)`. In Dart 3.13 and later, libraries can opt into concise constructor declarations that use `new` and `factory` as modifiers directly, with no class name and no dot: `new origin() : x = 0, y = 0;` and `factory clone(Point other) => Point(other.x, other.y);`. The full mapping table and its refactoring benefits are covered in `dart-new-factory-class-headers.md`, and Effective Dart now prefers the concise form (lint `unnecessary_type_name_in_constructor`) for language versions at or above 3.13. Note that this is about *declaring* constructors — writing `new Point()` at a call site is still discouraged by the `unnecessary_new` lint. Two companions round out modern construction: primary constructors put fields and constructor parameters on a single class header line (`dart-primary-constructors.md`), and super parameters forward arguments to a superclass constructor without repeating them (`dart-super-parameters.md`). Between the four constructor flavors and those three syntax tools, almost any initialization story can be told without duplication.

## Takeaways

- Named constructors name the *intent* of an alternate route, but they are not inherited by subclasses.
- Redirecting generative constructors (`: this(...)`) give your class a single source of initialization truth, and they can be `const` when their target is.
- Use `factory` when construction may return a cached instance or a subtype, or when real work must happen first — and never reach for `this` inside a factory body.
- Redirecting factories (`= Target`) preserve `const`-ness across an abstraction and avoid copying parameter lists.
- **Takeaway:** pick the constructor that states what is true about the object — named for intent, redirecting for reuse, factory for anything more complicated than `new`.

## References

- https://dart.dev/language/constructors
- https://dart.dev/effective-dart/usage
- https://dart.dev/language/classes
