dart-use-pattern-matching

Implements Dart switch expressions and pattern matching for destructuring and exhaustive type handling.

Updated Jan 8, 2026
One-click install
npx skills add https://github.com/arslan9024/White-Caves --skill dart-use-pattern-matching-arslan9024
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: dart-use-pattern-matching
Source: https://github.com/arslan9024/White-Caves/tree/main/.agents/skills/dart-use-pattern-matching
Command: npx skills add https://github.com/arslan9024/White-Caves --skill dart-use-pattern-matching-arslan9024

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Writing Dart code that validates and extracts data from JSON, records, or class hierarchies often leads to verbose, error-prone conditional logic. This Skill guides the correct use of Dart 3 pattern matching so data is validated, destructured, and handled exhaustively in fewer lines. ## Core Features & Use Cases - Pattern Selection Guidance: Chooses the right pattern type (Map, List, Record, Object, Relational, Logical) based on the data structure being matched. - Switch Construct Rules: Distinguishes when to use switch expressions for value production versus switch statements for side effects. - Exhaustiveness Verification: Provides a feedback loop using dart analyze to catch non-exhaustive matches over sealed classes and enums. - Use Case: When parsing a JSON payload like {'user': ['Lily', 13]}, apply a Map and List pattern to validate structure and bind name and age variables in a single if-case statement. ## Quick Start Refactor my Dart function that parses this JSON response to use pattern matching with a switch expression and verify exhaustiveness with dart analyze.

Frequently Asked Questions about dart-use-pattern-matching

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I use pattern matching in Dart?▼

Use switch expressions or if-case statements with patterns like Map, List, Record, or Object patterns to match and destructure data. For example, `if (data case {'user': [String name, int age]})` validates JSON structure and binds variables in one step.

When should I use a switch expression vs a switch statement in Dart?▼

Use a switch expression when producing a value, since each case is a single expression with no fallthrough and must be exhaustive. Use a switch statement when executing side effects, where non-empty cases implicitly break and empty cases fall through.

How do I destructure JSON data in Dart with patterns?▼

Combine Map and List patterns to validate structure and extract values simultaneously. A pattern like `{'user': [String name, int age]}` checks the keys and types while binding name and age to local variables.

Why does Dart say my switch is not exhaustively matched?▼

This error appears when switching over sealed classes or enums without covering all subtypes. Run dart analyze to find the missing cases, then add Object patterns for each unhandled subtype or a wildcard (_) case as a fallback.

Can Dart patterns match with conditions like ranges?▼

Yes, relational patterns (>=, <=, ==) match against constant expressions, and guard clauses using `when` evaluate arbitrary conditions after a pattern matches. Logical-and (&&) and logical-or (||) patterns combine multiple matchers in one case.