/flutter:dart3-idioms-and-coding-standardsfrom Flutter Skills
Enforces Dart 3 idioms: sealed classes with exhaustive switches, three class modifiers only, records as intra-layer tuples, immutable value types, total domain functions, and complexity limits. Use when authoring or reviewing Dart types and declarations.
Ships in Flutter Skills. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them; scripts run unread.
Modern Dart 3.x, value-type-first, total functions for domain logic. A language feature earns its place when it converts a runtime silence into a compile error; everything else is decoration. This skill governs how each declaration is typed, named, kept immutable, and kept total — the error-handling architecture that rides on top of these mechanics lives in error-handling-typed-results.
Read the reference for the task at hand:
references/construct-verdict-table.md — the feature-by-feature verdict table (sealed / enum / record / class modifiers / extension type / codegen packages), with the rationale for each Use / Skip.references/immutability-and-equality.md — immutable value types, copyWith, when to hand-write ==/hashCode, and stable-identity vs value-equality.references/complexity-and-honesty.md — the length/nesting limits with their evidence, and the late/!/dynamic honesty-dodge bans in full.Run scripts/check-dart3-idioms.sh before a PR.
switch on a sealed type or enum carries no default: and no case _:. A wildcard makes the switch compile forever, discarding the one compile-time guarantee the type exists for — adding a variant then falls through silently at runtime. Exhaustiveness is the whole product.sealed class for a closed variant set the compiler must exhaust; final class for every concrete leaf; abstract interface class for a seam a test fake implements. Default concrete types to final. Skip base, extension type, primary constructors, and macros.sealed hierarchy, payload-free closed sets as an enum. Start with enum; convert to sealed + final class the moment any member needs a field. Never bolt nullable fields onto an enum for data that applies to only some members — that turns every access into a null-check the compiler cannot reason about.final fields, a const constructor where legal, value equality, and copyWith to derive. Mutating a value handed to a widget is a rebuild-and-golden-test killer. Prefer final locals and const constructors everywhere the analyzer allows.final String id (or a typed id). Deriving identity from all fields collapses two distinct entities that happen to share values (two Items both named "Draft") into one.assert (stripped in release), never throw. Recoverable I/O failures return a typed result — see error-handling-typed-results.late to dodge nullability, no ! on a value that matters, no dynamic/Map<String, dynamic> as an ad-hoc model. Each hides a runtime failure the type system would otherwise force you to handle. Use ?./??/promotion for null, and a typed model for structured data.lowerCamelCase. UpperCamelCase types/extensions/enums; lowercase_with_underscores files/dirs/import prefixes; lowerCamelCase vars/params/methods/constants (maxItems, never MAX_ITEMS); acronyms over two letters capitalize as a word (JsonMap, HttpClient, not JSONMap). File name = its primary declaration.build() ≤ ~80, file ≤ ~300, class public API ≤ ~10 members, positional params ≤ 3, logic nesting ≤ 3. Widget build trees legitimately nest deeper — widget build nesting ≤ 5 is the one explicit exception (referenced by widget-composition). Refactor prompts, not laws — a cohesive overrun (a single state machine scattered across five fragments is worse) is justified in the PR.freezed when the boilerplate dominates. For a trivial immutable (1–3 fields) hand-write @immutable + const ctor + final fields (+ manual == / Object.hash when a map key). For a domain or UI-state value type where copyWith/==/hashCode/sealed-union boilerplate gets tedious, freezed is allowed and is the default — *.freezed.dart is a first-class generated artifact. Skip equatable (five lines of == + Object.hash cover it), fpdart/dartz (an Either erases exhaustiveness), and any --enable-experiment flag (an abandoned repo stops building the day the flag is dropped).Model a closed set of individually actionable cases as one sealed hierarchy in one file. sealed requires every subtype to live in the same library — the file is the closed set and the compiler enforces it.
@immutable
sealed class SyncOutcome {
const SyncOutcome();
}
final class SyncApplied extends SyncOutcome {
const SyncApplied(this.appliedCount);
final int appliedCount;
}
final class SyncSkipped extends SyncOutcome {
const SyncSkipped(this.reason);
final String reason;
}
Switch with no default: — adding a variant is then a compile error until every site handles it:
// RIGHT — exhaustive, no wildcard.
switch (outcome) {
case SyncApplied(:final appliedCount):
log.info('applied $appliedCount');
case SyncSkipped(:final reason):
log.warning('skipped: $reason');
}
A dropped branch reports as non_exhaustive_switch_statement from dart analyze and fails dart compile. Never suppress it; a suppressed analyzer diagnostic still fails the AOT build, so CI must build, not merely analyze.
| Intent | Declaration |
|---|---|
| A closed set of variants the compiler must exhaust | sealed class (implicitly abstract; not constructible or implementable outside its library) |
| A concrete leaf — a variant, a service impl, a value type | final class |
| A seam a test fake must satisfy | abstract interface class |
Default every concrete type to final class: it blocks both extension and implementation, so no subclass silently inherits half a behaviour and no test accidentally implements a concrete class to pick up its fields. abstract interface class says "implement me, don't extend me" — exactly the contract a fake wants; a bare abstract class permits extends, and an inherited default on a service interface is how a fake ends up quietly calling the real implementation. Skip base (it polices a library-boundary invariant a single-package app does not have) and extension type (an explicitly unsafe abstraction — the representation stays reachable).
Use a record for an ephemeral multi-value return inside a single layer: (int row, int col) as a local coordinate is ideal — structural equality free, no class, no codegen. Name the fields once a record survives more than a few lines: ({int row, int col}) reads at the use site; (int, int) is a swap bug the compiler cannot see.
// Intra-layer only. Two same-typed positional fields = a latent swap bug.
({int page, int size}) nextPage(({int page, int size}) current) =>
(page: current.page + 1, size: current.size);
Do not typedef a record shape into existence to dodge the class decision — a named shape used across files wants a class.
Start with an enum when the set is closed and every member is payload-free — a display category, a mode. Enums get the same compiler exhaustiveness in a switch and cost one line each. Convert to sealed + final class the moment any member needs a field.
enum OrderStatus { draft, submitted, shipped, cancelled }
// A member needs data -> sealed, not an enum with parallel nullable fields.
@immutable
sealed class Payment {
const Payment();
}
final class Cash extends Payment { const Cash(); }
final class Card extends Payment {
const Card(this.last4);
final String last4;
}
Prefer a switch expression when every arm produces a value and no arm has a statement body — exhaustive by construction, cannot fall through:
final label = switch (status) {
OrderStatus.draft => 'Draft',
OrderStatus.submitted => 'Submitted',
OrderStatus.shipped => 'Shipped',
OrderStatus.cancelled => 'Cancelled',
};
Use a switch statement when arms perform effects; do not contort effects into an expression with a void sink. Use if (x case Pattern) for a single interesting shape where a switch would be one real branch plus a dead one — but never if-case on a sealed type, which silently reintroduces the non-exhaustive hole a switch would have caught.
if (settings.retry case final RetryPolicy p when p.isEnabled) {
await scheduler.arm(p);
}
Four lines replace a code generator. Hand-write ==/hashCode only when the type is a map key or set member; carry a stable id for identity in lists.
@immutable
final class Task {
const Task({required this.id, required this.title, required this.done});
final String id; // identity — NOT derived from title/done
final String title;
final bool done;
Task copyWith({String? title, bool? done}) =>
Task(id: id, title: title ?? this.title, done: done ?? this.done);
@override
bool operator ==(Object other) =>
other is Task && other.id == id && other.title == title && other.done == done;
@override
int get hashCode => Object.hash(id, title, done);
}
A pure function returns for every input. Uncertainty is a value; a programmer invariant is an assert.
/// Total: clamps rather than throwing on an out-of-range page.
int clampPage(int requested, int pageCount) {
assert(pageCount >= 1, 'pageCount must be positive'); // programmer invariant
if (requested < 0) return 0;
if (requested >= pageCount) return pageCount - 1;
return requested;
}
default: or case _: on a sealed type or enum — discards exhaustiveness; a new variant falls through at runtime instead of failing to compile.if (x case SomeVariant()) on a sealed type — reintroduces the non-exhaustive hole a switch catches.enum with a constructor and nullable fields that apply to only some members — every access is a null-check the compiler cannot reason about; use sealed + final class.class Order extends ChangeNotifier used as a mutable domain model — domain values are immutable; mutate through a ViewModel (see state-management-riverpod).==-on-all-fields — two entities sharing values collapse into one; carry a stable id.late Foo foo; / foo!.value to dodge a null, Map<String, dynamic> as a model — hides a runtime failure; use ?./??/promotion and a typed model.throw from a pure domain function to signal a normal outcome — return the outcome; assert for programmer invariants only.const MAX_ITEMS / class JSONModel / final s = 3; — SCREAMING_CAPS, mis-cased acronym, abbreviation; use maxItems, JsonModel, full words.Widget _buildHeader() => ... — rebuilds with its parent, can't be const; extract a const widget class (see widget-composition).equatable/fpdart/dartz or an --enable-experiment flag — redundant weight or an exhaustiveness-erasing Either; hand-roll the four lines, or reach for freezed when the boilerplate genuinely dominates.switch on a sealed type or enum is exhaustive with no default:/case _:.final class; seams are abstract interface class; closed sets are sealed/enum; no base/extension type/experiment flags.sealed + final class; payload-free sets are enum; no nullable-field-tagged enums.final fields, const ctor where legal, copyWith); identity is a stable id, not equals-on-all-fields.assert.late/!/dynamic/Map<String,dynamic> honesty dodge on a value that matters.lowerCamelCase; file = its primary declaration.scripts/check-dart3-idioms.sh passes; dart analyze clean.error-handling-typed-results — the Result/Failure spine and global error net that ride on these sealed-type mechanics.naming-conventions — the role-suffix naming (Screen/Notifier/Repository/Service/Failure) that names the layer.state-management-riverpod — where mutable state lives (Notifier ViewModels over immutable state); domain values here stay immutable.widget-composition — extract const widget classes, not _buildX() methods.dart3-idioms-and-coding-standards is enforced in CI by lint-and-style-config and ci-pipeline-and-gates.if-case). https://dart.dev/language/patternsexamples/immutable_value.dart (opens in a new tab)examples/sealed_outcome.dart (opens in a new tab)examples/total_domain_function.dart (opens in a new tab)references/complexity-and-honesty.md (opens in a new tab)references/construct-verdict-table.md (opens in a new tab)references/immutability-and-equality.md (opens in a new tab)scripts/check-dart3-idioms.sh (opens in a new tab)What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
40 skills in this plugin
/accessibility-as-codeEnforces accessibility as a correctness property authored into each widget — Semantics(button/label) or ExcludeSemantics on every node, a11y state read from MediaQuery not app state, never MediaQuery.withClampedTextScaling / textScaleFactor / FittedBox / TextOverflow.ellipsis to fit a label, non-color redundant channels (icon+label+shape+text) for every state, contrast against composited backgrounds (4.5:1 body / 3:1 large), 44px single-tap targets, OrdinalSortKey traversal, and honoring boldText / reduce-motion. Use when adding a GestureDetector/InkWell or any tap target, adding an Icon or Image, reaching for withClampedTextScaling/FittedBox/ellipsis/textScaleFactor to make text fit, encoding state via color, sizing type, ordering focus traversal, or reviewing any View for screen-reader/switch/low-vision support./adaptive-layoutEnforces adapting layout by available CONSTRAINTS/size, never device or platform checks — LayoutBuilder + MediaQuery.sizeOf/paddingOf/viewInsetsOf (not .of) for narrow rebuilds, Material 3 window size classes (compact <600, medium 600-840, expanded 840-1200, large >1200) as the breakpoint vocabulary, navigation affordance chosen by width (NavigationBar → NavigationRail → NavigationDrawer), list-detail single-pane-vs-two-pane, readable max-width via ConstrainedBox, Flexible/Expanded/FractionallySizedBox over fixed widths, SafeArea + display cutouts + keyboard insets, never lock orientation, foldable/hinge awareness via MediaQuery.displayFeatures, and golden-matrix verification across sizes. Use when building responsive or adaptive UI, tablet/desktop/foldable support, master-detail or two-pane screens, a NavigationRail-vs-BottomNav shell, breakpoints, LayoutBuilder, MediaQuery sizing, SafeArea/cutouts, or fixing overflow at large widths.npx claudepluginhub zakariaf/flutter-skills --plugin flutterOther plugins' skills for the same job