This skill provides guidance on using pattern matching techniques in Dart programming. It's aimed at developers looking to enhance their coding efficiency by utilizing various pattern types for data validation and handling.
$ npx skills add https://github.com/dart-lang/skills --skill dart-use-pattern-matchingPattern matching in Dart enables developers to write more expressive and maintainable code by matching complex data structures and conditions in a single operation. This skill guides AI agents on when and how to apply switch expressions and pattern matching techniques across Dart projects, replacing verbose conditional logic with concise, readable patterns. Pattern matching excels at data validation, decomposing nested structures, and handling multiple cases elegantly. Developers benefit from reduced code complexity, improved readability, and fewer bugs. Use this skill when refactoring conditional logic or building new features that involve matching against structured data.
Install the skill using the provided command and follow the documentation for guidance.
Validating JSON structures using Map and List patterns.
Destructuring multiple return values with Record patterns.
Implementing type-specific behavior through Object patterns and sealed classes.
Matching numeric conditions effectively with Relational and Logical patterns.
$ npx skills add https://github.com/dart-lang/skills --skill dart-use-pattern-matchinggit clone https://github.com/dart-lang/skillsCopy the install command above and run it in your terminal.
Launch Claude Code, Cursor, or your preferred AI coding agent.
Use the prompt template or examples below to test the skill.
Adapt the skill to your specific use case and workflow.
Explain how to use Dart's pattern matching features to handle and validate [DATA_TYPE] data in [COMPANY]'s [PROJECT_NAME] application. Include examples of switch expressions, record patterns, and collection patterns. Focus on reducing boilerplate code and improving type safety.
# Dart Pattern Matching Guide for User Data Validation
## Overview
Dart 3.0+ introduced powerful pattern matching features that can significantly reduce boilerplate in your validation logic. Here’s how to apply them to user data in the `user_profile` module of our `social_app` project.
## Key Patterns Implemented
### 1. Switch Expressions for Validation
```dart
final validationResult = switch (userInput) {
(name: final name) when name.isEmpty => 'Name cannot be empty',
(age: final age) when age < 13 => 'Must be 13+ years old',
(email: final email) when !email.contains('@') => 'Invalid email format',
_ => 'Valid user data'
};
```
### 2. Record Patterns for Complex Data
```dart
final userData = ('John Doe', 28, '[email protected]');
final (name, age, email) = switch (userData) {
(_, int age, _) when age > 100 => throw Exception('Invalid age'),
(String name, _, String email) => (name.trim(), email.toLowerCase()),
};
```
### 3. Collection Patterns for Lists/Maps
```dart
final List<String> tags = ['dart', 'flutter', 'mobile'];
final filteredTags = switch (tags) {
['dart', ...final rest] => rest.where((t) => t.length > 5).toList(),
[_, 'flutter', _] => ['flutter-specific-tag'],
_ => throw Exception('Invalid tag combination')
};
```
## Performance Impact
- Reduced validation code by ~40% in our user onboarding flow
- Eliminated 12+ null checks through exhaustive pattern matching
- Improved type safety in data transformation pipelines
## Next Steps
1. Refactor our existing validation logic in `lib/validators/user.dart`
2. Add exhaustive pattern matching for all API response types
3. Document new patterns in the team’s coding standards guide
```Take a free 3-minute scan and get personalized AI skill recommendations.
Take free scan