dart-use-primary-constructors

Write and migrate Dart classes to primary constructor syntax with correct semantics.

1|Updated Aug 14, 2026
One-click install
npx skills add https://github.com/sohampawar1866/zeromile-go --skill dart-use-primary-constructors-sohampawar1866
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: dart-use-primary-constructors
Source: https://github.com/sohampawar1866/zeromile-go/tree/main/.agents/skills/dart-use-primary-constructors
Command: npx skills add https://github.com/sohampawar1866/zeromile-go --skill dart-use-primary-constructors-sohampawar1866

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Dart's primary constructors feature introduces new syntax and scoping rules that are easy to get wrong, from declaring parameters in class headers to handling initializer lists, late field restrictions, and redirecting constructors. This Skill provides the syntax reference, semantic rules, and migration workflows needed to write correct code and refactor legacy classes. ## Core Features & Use Cases - Syntax Reference: Covers declaring, initializing, and regular parameters, const primary constructors, extension types, empty-body semicolon shorthand, the in-body this : initializer list, and abbreviated new/factory constructor syntax. - Semantics & Diagnostics: Explains the Primary Initializer Scope, late variable restrictions, shadowing, double-initialization errors, and provides a troubleshooting table mapping error codes to fixes. - Migration Workflows: Step-by-step refactoring guides convert verbose traditional constructors into primary constructors and abbreviated in-body constructors. - Use Case: A developer upgrading a Flutter codebase to Dart 3.13 can refactor class User { final String name; User(this.name); } into class User(final String name); while correctly handling asserts, calculated fields, and redirecting named constructors. ## Quick Start Refactor my Dart class to use a primary constructor and explain any errors the analyzer reports.

Frequently Asked Questions about dart-use-primary-constructors

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I migrate a Dart class to a primary constructor?

Move the fields into the class header with final or var modifiers, replace an empty body with a semicolon, and move any asserts or initializer lists into a `this :` block in the body. Other generative constructors must redirect to the primary constructor.

What Dart version supports primary constructors?

Primary constructors are enabled by default in Dart 3.13 and above. In Dart 3.12 they are experimental and require the `--enable-experiment=primary-constructors` flag or the analyzer enable-experiment setting. Dart 3.11 and earlier do not support them.

Why can't I use a primary constructor parameter in a late field initializer?

The Primary Initializer Scope is not active for late instance variable initializers because late variables may be evaluated after construction completes. Make the field non-late or route the value through another non-late field.

Can a Dart class with a primary constructor have other constructors?

Yes, but all other generative constructors declared in the body must redirect directly or indirectly to the primary constructor. Declaring a non-redirecting generative constructor triggers the nonRedirectingGenerativeConstructorWithPrimary error.

How do extension types use primary constructors in Dart?

Extension types must use primary constructors, where the single header parameter is the representation field. The representation variable cannot use var, and final is inferred if omitted.

What is the abbreviated concise constructor syntax in Dart?

In-body constructors can omit the class name and use `new` or `factory` instead, such as `new()`, `new name()`, `const new()`, or `factory name() => ...`. This reduces repetition while keeping constructors inside the class body.