---
title: "Modeling Objects with Classes, Constructors, and Initializing Formals in Dart"
date: "2026-12-03T10:07:00+07:00"
description: "Model real objects in Dart with fields, constructors, this.x initializing formals, initializer lists, final fields, and const canonicalization."
author: "AI Assistant"
image: "https://blog.redlinesoft.net/images/post/dart-classes-constructors-initializing-formals.jpg"
alt: "Conceptual illustration of a class blueprint assembling data fields into a constructed object"
tags: ['dart', 'dart-language', 'oop', 'classes', 'constructors']
---

A class is your blueprint for a value, and the constructor is the doorway objects walk through — Dart gives you several elegant ways to build that doorway, from `this.x` initializing formals to initializer lists that run before your code ever executes.

## Fields and constructors

An object is a bundle of instance variables plus the methods that operate on them. A generative constructor creates a new instance and guarantees that every field ends up initialized before anyone can read it:

```dart
class Point {
  double x;
  double y;

  Point(this.x, this.y);

  @override
  String toString() => 'Point($x, $y)';
}

void main() {
  final p = Point(2, 3);
  print(p); // Point(2.0, 3.0)
}
```

Non-nullable fields like `x` and `y` *must* be initialized before the constructor body runs, which is exactly what the constructor is for. If you never declare one, Dart quietly supplies a default: an unnamed, argument-free generative constructor. And because the `new` keyword has been optional since Dart 2 (and is flagged by the `unnecessary_new` lint), `Point(2, 3)` and `new Point(2, 3)` mean the same thing — only write the former.

## Initializing formals beat manual assignment

To simplify the common pattern of copying a constructor argument into a field, Dart has *initializing formal parameters*: write `this.` before the parameter name and omit the body entirely.

```dart
class ManualPoint {
  double x;
  double y;
  ManualPoint(double x, double y) : x = x, y = y;
}

class Point {
  double x;
  double y;
  Point(this.x, this.y);
}
```

The first version types the name `x` four times to define one field; the second is the idiomatic form. Effective Dart codifies it as **DO use initializing formals when possible** (`prefer_initializing_formals`), and the surrounding rules keep it clean: never annotate the type of an initializing formal (`type_init_formals`), and reach for `this.` outside of formals only to redirect to another constructor or to disambiguate a shadowed member (`unnecessary_this`).

| Approach | Example | Verdict |
| --- | --- | --- |
| Initializing formal | `Point(this.x, this.y);` | Preferred — concise and safe |
| Manual initializer list | `Point(double x, double y) : x = x, y = y;` | Works, but repetitive |
| Assignment in the body | `Point(double x, double y) { this.x = x; }` | Too late for `final` fields |
| Type-annotated formal | `Point(double this.x, double this.y);` | Flagged by `type_init_formals` |

Initializing formals work for positional, optional positional, and named parameters alike, and they are one of the few ways to satisfy a non-nullable or `final` field directly from a parameter:

```dart
class PointB {
  final double x;
  final double y;
  PointB(this.x, this.y);
  PointB.optional([this.x = 0.0, this.y = 0.0]);
  PointB.named({this.x = 1.0, this.y = 1.0});
}

void main() {
  print(PointB.named(x: 2.0, y: 3.0)); // Instance of 'PointB'
}
```

## Initializer lists run before the body

When a field needs computation rather than a forwarded parameter, use an initializer list. It runs *before* the constructor body, so `final` fields can still be assigned there, and it is the correct place for development-time validation with `assert`:

```dart
import 'dart:math';

class Point {
  final double x;
  final double y;
  final double distanceFromOrigin;
  Point(double x, double y)
      : x = x,
        y = y,
        distanceFromOrigin = sqrt(x * x + y * y),
        assert(x >= 0 && y >= 0);
}

void main() {
  print(Point(2, 3).distanceFromOrigin); // 3.605551275463989
}
```

There is one hard restriction: **the right-hand side of an initializer list cannot access `this`**. You can read parameters, call static methods, and invoke other constructors — but no instance methods or fields exist yet. Effective Dart also prefers an initializer list over marking the field `late`: the list keeps the field's static safety and gives you the value before the body runs.

## Where a final field can get its value

Dart offers exactly three sanctioned homes for a `final` field's initial value, and Effective Dart adds a fourth rule — **DO initialize fields at their declaration when possible**:

```dart
class ProfileMark {
  final String name;
  final DateTime start = DateTime.now(); // 1. At declaration.
  ProfileMark(this.name); // 2. Initializing formal.
  ProfileMark.unnamed() : name = ''; // 3. Initializer list.
}
```

Initializing at the declaration takes less code and stops you duplicating the same value across several constructors — `start` above would otherwise be repeated in every one of them. A field initialized at its declaration cannot reference `this` (the initializer runs before the instance exists), unless you mark it `late`, whose initializer *can* use `this` because it runs after construction. If a `final` field genuinely must be computed once you know more about the instance, prefer a factory constructor that computes the value and delegates to a generative one — see `dart-named-factory-redirecting-constructors.md`.

## Const constructors and canonicalization

When a class is immutable — every field `final`, constructor doing nothing but initializing them — make the constructor `const`. That lets users create instances wherever constants are required: inside larger constants, switch cases, and default parameter values.

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

void main() {
  final a = const ImmutablePoint(1, 1);
  final b = const ImmutablePoint(1, 1);
  assert(identical(a, b)); // One canonical instance.
  print(identical(a, b)); // true
}
```

Two compile-time constants with equal fields are *canonicalized* to a single object, so `identical` holds. Note that invoking a `const` constructor *without* `const` outside a constant context creates an ordinary non-constant object — the keyword, or the surrounding constant context, is what opts you in.

## Instance variables create implicit getters and setters

Every instance variable generates an implicit getter, and every non-`final` variable also generates an implicit setter. With `class Point { double x = 0; }`, the statement `p.x = 5` invokes the setter while reading `p.x` invokes the getter — functionally identical to calling tiny functions the compiler synthesizes for you on every property access.

That indirection is the foundation of encapsulation: you can later replace a plain field with a computed or validated property without touching a single call site. The full story — explicit `get`/`set`, validation, read-only properties, and the lints that guard them — lives in `dart-getters-setters.md`.

## Takeaways

- Fields must be initialized before the constructor body runs; `this.x` initializing formals do it in the least code possible.
- Initializer lists run before the body, cannot touch `this` on their right-hand sides, and are the right home for `assert` validation and computed `final` values.
- Initialize fields at their declaration whenever they do not depend on constructor parameters, and reserve `late` for the rare case that needs `this`.
- `const` constructors plus `final` fields buy you canonicalized, `identical` instances.
- **Takeaway:** model an object as immutable-by-default fields wired up by initializing formals, with initializer lists reserved for validation and computation.

## References

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