gsmlg-dev/code-agent/dart-use-pattern-matching
Claude Code and agent skill by gsmlg-dev/code-agent
- community
Checking that the SKILL.md file is still in the repository.
Use switch expressions and pattern matching where appropriate
Install
npx vskill@latest install gsmlg-dev/code-agent/dart-use-pattern-matchingSKILL.md
The same SKILL.md was published earlier as dart-lang/skills/dart-use-pattern-matching.
Implementing Dart Patterns
Contents
- Pattern Selection Strategy
- Switch Statements vs. Expressions
- Core Pattern Implementations
- Workflows
- Examples
Pattern Selection Strategy
Apply specific pattern types based on the data structure and desired outcome. Follow these conditional guidelines:
- If validating and extracting from deserialized data (e.g., JSON): Use Map and List patterns to simultaneously check structure and destructure key-value pairs.
- If handling multiple return values: Use Record patterns to destructure fields directly into local variables.
- If executing type-specific behavior (Algebraic Data Types): Use Object patterns combined with
sealedclasses to ensure exhaustiveness. - If matching numeric ranges or conditions: Use Relational (
>=,<=) and Logical-and (&&) patterns. - If multiple cases share logic: Use Logical-or (
||) patterns to share a single case body or guard clause. - If ignoring specific values: Use the Wildcard pattern (
_) or a non-matching Rest element (...) in collections.
Switch Statements vs. Expressions
Select the appropriate switch construct based on the execution context:
- If producing a value: Use a switch expression.
- Syntax:
switch (value) { pattern => expression, } - Rule: Each case must be a single expression. No implicit fallthrough. Must be exhaustive.
- If executing statements or side effects: Use a switch statement.
- Syntax:
switch (value) { case pattern: statements; } - Rule: Empty cases fall through to the next case. Non-empty cases implicitly break (no
breakkeyword required).
Core Pattern Implementations
Implement patterns using the following syntax and rules:
- Logical-or (`||`):
pattern1 || pattern2. Both branches must define the exact same set of variables. - Logical-and (`&&`):
pattern1 && pattern2. Branches must not define overlapping variables. - Relational:
==,!=,<,>,<=,>=followed by a constant expression. - Cast (`as`):
pattern as Type. Throws if the value does not match the type. Use to forcibly assert types during destructuring. - Null-check (`?`):
pattern?. Fails the match if the value is null. Binds the variable to the non-nullable base type. - Null-assert (`!`):
pattern!. Throws if the value is null. - Variable:
var nameorType name. Binds the matched value to a new local variable. - Wildcard (`_`): Matches any value and discards it.
- List:
[pattern1, pattern2]. Matches lists of exact length unless a Rest element (...or...var rest) is used. - Map:
{"key": pattern}. Matches maps containing the specified keys. Ignores unmatched keys. - Record:
(pattern1, named: pattern2). Matches records of the exact shape. Use:var nameto infer the getter name. - Object:
ClassName(field: pattern). Matches instances ofClassName. Use:var fieldto infer the getter name.
Workflows
Task Progress: Implementing Pattern Matching
Copy this checklist to track progress when implementing complex pattern matching logic:
- [ ] Identify the data structure being evaluated (JSON, Record, Class, Enum).
- [ ] Select the appropriate switch construct (Expression for values, Statement for side-effects).
- [ ] Define the required patterns (Object, Map, List, Record).
- [ ] Extract required data using Variable patterns (
var x,:var y). - [ ] Apply Guard clauses (
when condition) for logic that cannot be expressed via patterns. - [ ] Handle unmatched cases using a Wildcard (
_) ordefaultclause (if not using a sealed class). - [ ] Run exhaustiveness validator.
Feedback Loop: Exhaustiveness Checking
When switching over sealed classes or enums, you must ensure all subtypes are handled.
- Run validator: Execute
dart analyze. - Review errors: Look for "The type 'X' is not exhaustively matched by the switch cases" errors.
- Fix: Add the missing Object patterns for the unhandled subtypes, or add a Wildcard (
_) case if a default fallback is acceptable.
Examples
JSON Validation and Destructuring
Use Map and List patterns to validate structure and extract data in a single step.
Input:
Implementation:
Algebraic Data Types (Sealed Classes)
Use Object patterns with switch expressions to handle family types exhaustively.
Implementation:
Variable Swapping and Destructuring
Use variable assignment patterns to swap values or extract record fields without temporary variables.
Implementation:
Guard Clauses and Logical-or
Use when to evaluate arbitrary conditions after a pattern matches.
Implementation:
Security
- Security scanPassed, 90 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingplugins/flutter-skills/skills/dart-use-pattern-matching/SKILL.md
Popularity
- 7-day trend score
- 0
Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/gsmlg-dev/code-agent/dart-use-pattern-matching)