원클릭으로
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 직업 분류 기준
Use when designing UI with OpenPencil — creating layouts via op CLI, batch design DSL, or MCP tools. Covers PenNode schema, semantic roles, typography, color, spacing, and common component patterns.
Work with Figma .fig design files and the running OpenPencil editor — inspect structure, query nodes, analyze design tokens, export images/SVG/JSX, and modify designs programmatically. Use when asked to open, inspect, export, analyze, or edit .fig files, or to control the running OpenPencil app.
Use when creating, updating, prioritizing, or querying GitHub Project todo items for omni-stream-ai/omni-code, especially backlog drafts, issue promotion, status changes, labels, and priority edits.
Write and organize unit tests for functions, methods, and classes using `package:test`. Use when creating new logic or fixing bugs to ensure code remains correct and regression-free.
Entrypoint structure, exit codes, cross-platform scripts. Use when building command line utilities, scripts, or applications.
Collect coverage using the coverage packge and create an LCOV report
| 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');
}