Skip to main content
The wildcard pattern (_) is a non-binding pattern that successfully matches any value or type without assigning it to a variable. It acts as an explicit discard mechanism during pattern matching, destructuring, and variable declaration, signaling to the compiler that the matched data is intentionally dropped from the lexical scope.

Syntax and Mechanics

The wildcard pattern is represented by a single underscore (_). Because it does not bind to a variable, it can be used multiple times within the same pattern or scope without causing naming collisions.

Standalone Wildcard

When used on its own, the wildcard matches any value. In switch statements or expressions, it functions as the exhaustive catch-all (replacing the legacy default keyword).

Typed Wildcard

The wildcard can be prefixed with a type annotation. This creates a pattern that matches only if the value is of the specified type, but still discards the value itself.

Destructuring Collections

In list, map, and record patterns, the wildcard is used to structurally match the shape of the collection while selectively ignoring specific positional or named elements.

Technical Characteristics

  1. Non-Binding Nature: In pattern contexts (introduced in Dart 3.0), _ is strictly a structural placeholder. Attempting to read or reference _ as a variable after it has been used in a pattern results in a compile-time error.
  2. Multiplicity: Unlike standard variable identifiers, _ can be repeated infinitely within the same destructuring assignment or pattern match.
  3. Logical Operators: The wildcard is frequently combined with logical-or (|) and logical-and (&) patterns to satisfy structural requirements without polluting the local scope with unused variables.
Tired of Poor Dart Skills? Fix That With Deep Grasping!Learn More