Your Site Title

Pattern

two possible jobs, matching data and possibly destructuring that data.

Pattern type Exported
const null, true, false, const 10, ‘abc’
list [‘a’ || ‘b’, var c ]
map {“key”: subpattern}
Object MyClass(…)
wildcard _
record (subpattern1, subpattern2)

pattern matching

Dart

refutable contexts if-case statement, switch statement, switch expression.

switch (data) {
    case PATTERN_01 when CONDITIONAL:
    // code
    case PATTERN_02 when CONDITIONAL:
    // other code
    default:
    // Last resort code
}

final value = switch(data) {
    PATTERN_01 => // code,
    PATTERN_02 => // other code,
    _ => // Last resort code,
};

if (variable case PATTERN) {

}

destructuring

var (String name, Color color) = ('Rose', Colors.red);
(name, color) = ('Orchid', Colors.white);
(String, Color) flowerAndColor() => ('Rose', Colors.red);

Reference