Primary Constructors

Understand the Concept

Minimum Dart language version: 3.13.

Purpose: Declare fields + primary constructor in one line at the top of the class, drastically reducing boilerplate code. Its runtime behavior is identical to traditional constructors; only the syntax is simplified.

  1. Declaring parameters: Header parameters marked with var / final automatically generate instance fields with matching names.
  2. Non-declaring parameters: Header parameters without var/final are merely constructor inputs and do not create class fields.
  3. In-body constructors: Named, factory, or redirecting constructors written inside the class braces.
  4. Mandatory rule: A class with a primary constructor cannot have any other non-redirecting generative constructors, ensuring all instances pass through the primary constructor.

Traditional Syntax vs Primary Constructor Shorthand

// Traditional verbose syntax
class Point {
  int x;
  int y;
  Point(this.x, this.y);
}

// Primary constructor shorthand
class Point(var int x, var int y);Code language: JavaScript (javascript)

Rules for Automatic Field Generation

1. With var / final

Instance fields are created automatically.

Write field variables directly inside parentheses after the class name.

// x and y become class member variables automatically
class Point(var int x, final int y);

void main() {
  final p = Point(10, 20);
  print(p.x); // 10
  print(p.y); // 20
  // p.y = 30; // Error: y is declared final
}Code language: JavaScript (javascript)

The above code throws an error when executed.

D:\dartdemo\firstdart\bin>dart run
Building package executable...
Failed to build firstdart:firstdart:
firstdart.dart:2:13: Error: This requires the experimental 'primary-constructors' language feature to be enabled.
Try passing the '--enable-experiment=primary-constructors' command line option.
class Point(var int x, final int y);
            ^^^
firstdart.dart:2:12: Error: This requires the experimental 'primary-constructors' language feature to be enabled.
Try passing the '--enable-experiment=primary-constructors' command line option.
class Point(var int x, final int y);
           ^
firstdart.dart:2:36: Error: This requires the experimental 'primary-constructors' language feature to be enabled.
Try passing the '--enable-experiment=primary-constructors' command line option.
class Point(var int x, final int y);Code language: JavaScript (javascript)

Cause: The shorthand syntax class Point(var int x, final int y); called Primary Constructors is an experimental feature of Dart. It is disabled by default in the current stable Dart release, so direct execution produces a syntax error.

Current date: July 10, 2026, 14:55. By the time readers view this article, Dart may have released a new official version that enables this syntax by default.

Use this command for temporary testing:

dart run --enable-experiment=primary-constructors

2. Without var / final

Only temporary parameters; no fields are generated.

class User(String name) {
  // print(name); // Parameters can be accessed directly inside the class, but no this.name field is created
  final String msg = "Hi $name"; // Compile error
}

void main() {
  final u = User("Tom");
  // print(u.name); // Compile error: field name does not exist
}Code language: JavaScript (javascript)

Previous:

Leave a Reply

Your email address will not be published. Required fields are marked *