Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The logical-and pattern (&&) is a composite pattern in Dart that matches a value if and only if both of its subpatterns successfully match the same value. It allows developers to apply multiple constraints, type checks, or destructuring operations to a single expression simultaneously.

Syntax

The pattern is constructed using the && operator between two subpatterns:
leftSubpattern && rightSubpattern

Evaluation Mechanics

  1. Shared Target: Both leftSubpattern and rightSubpattern evaluate against the exact same matched value.
  2. Left-to-Right Execution: The pattern evaluates the leftSubpattern first.
  3. Short-Circuiting: If the leftSubpattern fails to match, the entire logical-and pattern fails immediately. The rightSubpattern is never evaluated.
  4. Success Condition: The pattern as a whole succeeds only if the left subpattern succeeds and the right subpattern succeeds.

Variable Binding Rules

When using variable patterns within a logical-and pattern, the following strict scoping and binding rules apply:
  • Union of Bindings: The variables bound by the logical-and pattern are the union of the variables bound by both the left and right subpatterns.
  • No Shadowing/Collisions: It is a compile-time error if both subpatterns attempt to bind a variable with the exact same name.
  • Scope Availability: Variables bound in the left subpattern are not available for use within the right subpattern’s evaluation logic, but all bound variables from both sides are exposed to the resulting case body if the entire pattern matches.

Structural Examples

Combining a Type Test with a Relational Pattern The left subpattern binds the value to a typed variable, while the right subpattern applies a relational constraint to the same value.
switch (value) {
  case int x && >= 10:
    // Matches if 'value' is an integer AND is greater than or equal to 10.
    // 'x' is bound to the matched integer.
    break;
}
Multiple Destructuring on a Single Value The logical-and pattern can extract different properties from the same object or record simultaneously by applying multiple object or record patterns.
switch (pointRecord) {
  case (int x, _) && (_, int y):
    // Matches if the record has an integer in the first position AND an integer in the second.
    // Binds 'x' from the left subpattern and 'y' from the right subpattern.
    break;
}
Chaining Multiple Logical-And Patterns The && operator is left-associative and can be chained to enforce more than two constraints or extractions on the same value. When extracting properties from a class instance within a chain, an object pattern with an explicit type name must be used.
if (value case List<int> list && List<int>(length: var len) && [_, var second, ...]) {
  // 1. Type test and variable binding: Checks if value is List<int> and binds to 'list'
  // 2. Object pattern property extraction: Matches List<int>, extracts the 'length' getter, and binds to 'len'
  // 3. List pattern destructuring: Ensures at least two elements and binds the second to 'second'
}
Master Dart with Deep Grasping Methodology!Learn More