بنقرة واحدة
dart-use-pattern-matching
Use switch expressions and pattern matching where appropriate
التثبيت باستخدام Codex أو Claude انسخ هذا Prompt والصقه في Codex أو Claude أو مساعد آخر ليراجع صفحة Skill ويثبّتها لك.
القائمة
Use switch expressions and pattern matching where appropriate
التثبيت باستخدام Codex أو Claude انسخ هذا Prompt والصقه في Codex أو Claude أو مساعد آخر ليراجع صفحة Skill ويثبّتها لك.
استنادا إلى تصنيف SOC المهني
Guidelines and format for writing pull request descriptions in this repository. Use this skill whenever the user asks you to draft a pull request description, submit a PR, or update a PR description.
Validates an in-progress PR or feature branch of dart_skills_lint against known downstream ecosystem consumers. Use when assessing breaking changes across external repositories during PR evaluation, testing migrations against the changelog, or determining necessary backwards compatibility shims.
How to integrate, update, and configure the dart_skills_lint validation tool within a repository. Make sure to use this skill whenever the user asks to update dart_skills_lint, configure skills validation tests, fix skills linter dependency drifts, verify repository state before editing, optimize lint rules execution, or draft pull request submission commands.
Use this skill when you need to validate AI agent skills with dart_skills_lint — running the linter, interpreting failures, fixing violations, and authoring custom rules.
A deliberately broken fixture used by example/README.md to show what each rule's error output looks like.
Reference fixture for dart_skills_lint. Demonstrates a SKILL.md that passes every default rule: hyphen-lowercase name matching the parent directory, a properly sized description, and no other frontmatter fields that would trigger the disallowed-field check.
| name | dart-use-pattern-matching |
| description | Use switch expressions and pattern matching where appropriate |
| metadata | {"model":"models/gemini-3.1-pro-preview","last_modified":"Fri, 24 Apr 2026 15:08:55 GMT"} |
Apply specific pattern types based on the data structure and desired outcome. Follow these conditional guidelines:
sealed classes to ensure exhaustiveness.>=, <=) and Logical-and (&&) patterns.||) patterns to share a single case body or guard clause._) or a non-matching Rest element (...) in collections.Select the appropriate switch construct based on the execution context:
switch (value) { pattern => expression, }switch (value) { case pattern: statements; }break keyword required).Implement patterns using the following syntax and rules:
||): pattern1 || pattern2. Both branches must define the exact same set of variables.&&): pattern1 && pattern2. Branches must not define overlapping variables.==, !=, <, >, <=, >= followed by a constant expression.as): pattern as Type. Throws if the value does not match the type. Use to forcibly assert types during destructuring.?): pattern?. Fails the match if the value is null. Binds the variable to the non-nullable base type.!): pattern!. Throws if the value is null.var name or Type name. Binds the matched value to a new local variable._): Matches any value and discards it.[pattern1, pattern2]. Matches lists of exact length unless a Rest element (... or ...var rest) is used.{"key": pattern}. Matches maps containing the specified keys. Ignores unmatched keys.(pattern1, named: pattern2). Matches records of the exact shape. Use :var name to infer the getter name.ClassName(field: pattern). Matches instances of ClassName. Use :var field to infer the getter name.Copy this checklist to track progress when implementing complex pattern matching logic:
var x, :var y).when condition) for logic that cannot be expressed via patterns._) or default clause (if not using a sealed class).When switching over sealed classes or enums, you must ensure all subtypes are handled.
dart analyze._) case if a default fallback is acceptable.Use Map and List patterns to validate structure and extract data in a single step.
Input:
var data = {
'user': ['Lily', 13],
};
Implementation:
if (data case {'user': [String name, int age]}) {
print('User $name is $age years old.');
} else {
print('Invalid JSON structure.');
}
Use Object patterns with switch expressions to handle family types exhaustively.
Implementation:
sealed class Shape {}
class Square implements Shape {
final double length;
Square(this.length);
}
class Circle implements Shape {
final double radius;
Circle(this.radius);
}
// Switch expression guarantees exhaustiveness due to `sealed` modifier.
double calculateArea(Shape shape) => switch (shape) {
Square(length: var l) => l * l,
Circle(:var radius) => math.pi * radius * radius,
};
Use variable assignment patterns to swap values or extract record fields without temporary variables.
Implementation:
var (a, b) = ('left', 'right');
(b, a) = (a, b); // Swap values
// Destructuring a function return
var (name, age) = getUserInfo();
Use when to evaluate arbitrary conditions after a pattern matches.
Implementation:
switch (shape) {
case Square(size: var s) || Circle(size: var s) when s > 0:
print('Valid symmetric shape with size $s');
case Square() || Circle():
print('Invalid or empty shape');
default:
print('Unknown shape');
}