dart-use-pattern-matching

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

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

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 sealed classes or complex data structures like JSON. This Skill guides the correct use of Dart 3 pattern matching so code stays concise, type-safe, and exhaustively checked. ## Core Features & Use Cases - Pattern Selection Strategy: Choose the right pattern type (Map, List, Record, Object, Relational, Logical) based on the data structure being evaluated. - Switch Construct Guidance: Decide between switch expressions for value production and switch statements for side effects, with correct syntax rules. - Exhaustiveness Validation: A feedback loop using dart analyze to catch unhandled subtypes in sealed class hierarchies. - 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 switch expressions?▼

Use the syntax `switch (value) { pattern => expression}` where each case maps a pattern to a single expression. Switch expressions must be exhaustive and have no implicit fallthrough, making them ideal for producing values from sealed classes or enums.

How to destructure JSON data with Dart patterns?▼

Use Map and List patterns to validate structure and extract values simultaneously, for example `if (data case {'user': [String name, int age]})`. This checks the keys and types while binding name and age to local variables in one step.

What is the difference between Dart switch statements and switch expressions?▼

Switch expressions produce a value, require exhaustiveness, and allow only single expressions per case. Switch statements execute side effects, allow fallthrough on empty cases, and implicitly break on non-empty cases without a break keyword.

Does Dart pattern matching enforce exhaustiveness on sealed classes?▼

Yes, switching over sealed classes or enums requires all subtypes to be handled. Run `dart analyze` to find errors like 'not exhaustively matched', then add the missing Object patterns or a Wildcard case as a fallback.

When should I use guard clauses with Dart patterns?▼

Use a `when` clause after a pattern to evaluate arbitrary conditions that patterns cannot express, such as `case Square(size: var s) when s > 0`. Guards run only after the pattern matches and can reference bound variables.