npx skills add ...
npx skills add flutter/agent-plugins --skill dart-use-pattern-matching
npx skills add flutter/agent-plugins --skill dart-use-pattern-matching
Use switch expressions and pattern matching where appropriate
The same skill content is published under more than one repo. The install counts are split across them; any of these commands works.
Apply specific pattern types based on the data structure and desired outcome. Follow these conditional guidelines:
sealed classes to ensure exhaustiveness.>=, <=) and Logical-and (&&) patterns.||) patterns to share a single case body or guard clause._) or a non-matching Rest element (...) in collections.Select the appropriate switch construct based on the execution context:
switch (value) { pattern => expression, }switch (value) { case pattern: statements; }break keyword required).Implement patterns using the following syntax and rules:
||): pattern1 || pattern2. Both branches must define the exact same set of variables.&&): pattern1 && pattern2. Branches must not define overlapping variables.==, !=, <, >, <=, >= followed by a constant expression.as): pattern as Type. Throws if the value does not match the type. Use to forcibly assert types during destructuring.?): pattern?. Fails the match if the value is null. Binds the variable to the non-nullable base type.!): pattern!. Throws if the value is null.var name or Type name. Binds the matched value to a new local variable._): Matches any value and discards it.[pattern1, pattern2]. Matches lists of exact length unless a Rest element (... or ...var rest) is used.{"key": pattern}. Matches maps containing the specified keys. Ignores unmatched keys.(pattern1, named: pattern2). Matches records of the exact shape. Use :var name to infer the getter name.ClassName(field: pattern). Matches instances of ClassName. Use :var field to infer the getter name.Copy this checklist to track progress when implementing complex pattern matching logic:
var x, :var y).when condition) for logic that cannot be expressed via patterns._) or default clause (if not using a sealed class).When switching over sealed classes or enums, you must ensure all subtypes are handled.
dart analyze._) case if a default fallback is acceptable.Use Map and List patterns to validate structure and extract data in a single step.
Input:
Implementation:
Use Object patterns with switch expressions to handle family types exhaustively.
Implementation:
Use variable assignment patterns to swap values or extract record fields without temporary variables.
Implementation:
Use when to evaluate arbitrary conditions after a pattern matches.
Implementation:
sealed class Shape {}
class Square implements Shape {
final double length;
Square(this.length);
}
class Circle implements Shape {
final double radius;
Circle(this.radius);
}
// Switch expression guarantees exhaustiveness due to `sealed` modifier.
double calculateArea(Shape shape) => switch (shape) {
Square(length: var l) => l * l,
Circle(:var radius) => math.pi * radius * radius,
};var (a, b) = ('left', 'right');
(b, a) = (a, b); // Swap values
// Destructuring a function return
var (name, age) = getUserInfo();switch (shape) {
case Square(size: var s) || Circle(size: var s) when s > 0:
print('Valid symmetric shape with size $s');
case Square() || Circle():
print('Invalid or empty shape');
default:
print('Unknown shape');
}