dart-use-primary-constructors
БесплатноБез исполняемых скриптовНе проверенHelp users write syntactically and semantically correct primary constructors in Dart, and migrate/use the new constructor syntax, empty-body semicolon syntax…
от flutter★ 3 224 звезды на GitHubОб этом скилле
Dart Primary Constructors & New Constructor Syntax Skill
Use this skill when helping users write, refactor, or debug code using Dart's Primary Constructors feature.
Dart Version Requirements
- Dart 3.13 and above: Primary constructors are enabled by default.
- Dart 3.12: The feature is available but experimental. Users must explicitly enable the experiment flag
primary-constructorsvia--enable-experiment=primary-constructorsor inanalysis_options.yaml:
analyzer:
enable-experiment:
- primary-constructors
- Dart 3.11 and earlier: Primary constructors are not supported.
1. Overview
Primary Constructors allow developers to declare a non-redirecting generative constructor as well as a set of instance variables directly in the class header. This significantly reduces boilerplate and improves code readability.
Key Benefits
- Combines field declaration, parameter declaration, and initialization into a single declaration known as a declaring parameter declaration.
- Enables safe reference to constructor parameters in non-late field initializers (Primary Initializer Scope).
- Allows empty declaration bodies to be represented concisely with a semicolon (
;). - Introduces abbreviated concise syntax for in-body constructors.
2. Syntax Reference
2.1 Basic Class Header Syntax
To declare a primary constructor, place a parameter list immediately after the type name (and optional type parameters):
// Declares fields x and y, and a generative constructor Point(this.x, this.y)
class Point(var int x, var int y);
// Declares final fields
class PointFinal(final int x, final int y);
2.2 Declaring, Initializing, and Plain Parameters
A primary constructor parameter list distinguishes between three types of parameters:
- Declaring Parameters: Indicated by the
varorfinalmodifier (e.g.,final int x). They implicitly create a corresponding instance field in the class. - Initializing Parameters: Indicated by the
this.orsuper.prefix (e.g.,this.xorsuper.x). They initialize an existing field or a super constructor parameter, respectively. - Regular Parameters: Declared without modifiers (e.g.,
int y). They do not become fields and are only available during initialization (e.g., in field initializers or thethis :initializer list in the class body).
// `x` is a field and a parameter because it has the keyword `final`. In particular, we can use the name `x` in the initializer list in the in-body part of the primary constructor. 'y' is a only parameter because it has neither of the keywords `final` or `var`, but `y` is passed to the super constructor via the `this :` initializer list.
class C(final int x, int y) extends Base {
this : super(y);
}
Declaring parameters and initializing parameters are two ways of achieving the same goal: declaring a class with instance fields which are set in the constructor. Regular parameters are different in that their values are not automatically routed to an instance field.
2.3 Constant Primary Constructors
To make a primary constructor const, place the const keyword before the class/type name in the declaration header:
class const Point(final int x, final int y);
extension type const Ext(int x);
enum const MyEnum(final int x) {
entry(1);
}
2.4 Extension Types
Extension types must use primary constructors.
- The single parameter in the header is the representation field.
- The representation variable cannot use the
varmodifier (usingvartriggers therepresentation_field_modifiererror). - The representation variable can optionally use the
finalmodifier. Iffinalis not present then it is inferred; that is, the parameter is declaring whether or not it's explicitlyfinal.
2.5 Empty Body Semicolon Shorthand (;)
When a class, mixin class, mixin, extension or extension type has an empty body, the {} braces can be replaced by a semicolon (;):
class C(int x);
mixin class MC;
extension type ET(int x);
mixin M;
extension Ext on C;
2.6 The In-Body Part of a Primary Constructor (this ...)
If a primary constructor requires assertions or custom field initializations, they can be declared in the body using the this : syntax:
class Point(var int x, var int y) {
// Initializer list in class body
this : assert(x >= 0), y = y * 2;
}
You can also write a constructor body with this syntax (this {...}).
2.7 Abbreviated Concise Constructor Syntax
For constructors declared within the class body, the class name can be omitted and replaced with the new or factory keywords:
| Traditional Syntax | Abbreviated Concise Syntax |
|---|---|
MyClass() {} |
new() {} |
MyClass.name() {} |
new name() {} |
const MyClass(); |
const new(); |
const MyClass.name(); |
const new name(); |
factory MyClass() => ... |
factory() => ... |
| `factory My |
Установить dart-use-primary-constructors в Claude Code и Claude Desktop
Зарегайся, чтобы установить скилл
Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.
- Открой команду установки в одну строку
- Сохраняй скиллы в синхронизируемую библиотеку
- Уведомления, когда скиллы обновляются
Разрешённые инструменты
Инструменты, которые скиллу разрешено вызывать.
Без ограничений — скилл может использовать любой инструмент.
FAQ
Что делает скилл dart-use-primary-constructors?
Help users write syntactically and semantically correct primary constructors in Dart, and migrate/use the new constructor syntax, empty-body semicolon syntax, in-body initializer list syntax, and abbreviated concise constructor syntax.
Как установить скилл dart-use-primary-constructors?
Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.
Скилл dart-use-primary-constructors запускает скрипты?
Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.
Похожие скиллы
doc-coauthoring
Guide users through a structured workflow for co-authoring documentation. Use when user wants to write documentation, proposals, technical specs, decision…
от Anthropicacademy-guide
Stop and check this skill before finishing any reply to a question about how to use Claude or a Claude product — it recommends matching courses, tutorials…
от Anthropicinternal-comms
A set of resources to help me write all kinds of internal communications, using the formats that my company likes to use. Claude should use this skill…
от Anthropicskill-development
This skill should be used when the user wants to "create a skill", "add a skill to plugin", "write a new skill", "improve skill description", "organize skill…
от Anthropic