dart-use-pattern-matching

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

1|Updated Aug 14, 2026
One-click install
npx skills add https://github.com/sohampawar1866/zeromile-go --skill dart-use-pattern-matching-sohampawar1866
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: dart-use-pattern-matching
Source: https://github.com/sohampawar1866/zeromile-go/tree/main/.agents/skills/dart-use-pattern-matching
Command: npx skills add https://github.com/sohampawar1866/zeromile-go --skill dart-use-pattern-matching-sohampawar1866

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Writing conditional logic in Dart often leads to verbose if-else chains, manual type casts, and missed cases when handling JSON data, records, or sealed class hierarchies. This Skill guides the correct use of Dart 3 pattern matching so code stays concise, type-safe, and exhaustive. ## Core Features & Use Cases - Pattern Selection Strategy: Choose the right pattern type (Map, List, Record, Object, Relational, Logical) based on the data structure being matched. - Switch Construct Guidance: Decide between switch expressions for value production and switch statements for side effects, with correct syntax rules. - Exhaustiveness Validation: Workflow for running dart analyze to catch non-exhaustive switch errors on sealed classes and enums. - Use Case: When parsing a JSON response like {'user': ['Lily', 13]}, use a single if (data case {'user': [String name, int age]}) check to validate structure and destructure values in one step instead of nested casts. ## Quick Start Refactor my Dart function that handles API responses to use switch expressions and sealed class pattern matching with exhaustive cases.

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 with patterns like `switch (value) { pattern => result }` to match and destructure data. Dart supports Map, List, Record, Object, Relational, and Logical patterns, plus guard clauses with `when` for extra conditions.

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 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 validate and destructure JSON in Dart?▼

Use Map and List patterns in a single case, such as `if (data case {'user': [String name, int age]})`. This simultaneously checks the structure and binds the values to typed local variables without manual casts.

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`, then add Object patterns for the missing subtypes or a Wildcard `_` case as a default fallback.

Can Dart patterns match sealed class hierarchies?▼

Yes, Object patterns like `Square(length: var l)` match instances of sealed class subtypes, and the compiler guarantees exhaustiveness. This makes sealed classes with switch expressions the idiomatic way to model algebraic data types in Dart.