dart-use-pattern-matching
This skill contains shell command directives (!`command`) that may execute system commands. Review carefully before installing.
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.
More from dart-lang/skills
dart-add-unit-test
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.
2.1Kdart-fix-runtime-errors
Uses get_runtime_errors and lsp to fetch an active stack trace, locate the failing line, apply a fix, and verify resolution via hot_reload.
2.1Kdart-run-static-analysis
Execute `dart analyze` to identify warnings and errors, and use `dart fix --apply` to automatically resolve mechanical lint issues. Use during development to ensure code quality and before committing changes.
2.1Kdart-resolve-package-conflicts
Workflow for fixing package version conflicts. Use this when `pub get` fails due to incompatible package versions.
2.0Kdart-collect-coverage
Collect coverage using the coverage packge and create an LCOV report
2.0Kdart-generate-test-mocks
Define and generate mock objects for external dependencies using `package:mockito` and `build_runner`. Use when unit testing classes that depend on complex external services like APIs or databases.
2.0K