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 if-case statement in Dart is a control flow construct introduced in Dart 3.0 that integrates pattern matching directly into conditional logic. It evaluates whether a given expression conforms to a specific pattern, conditionally executing the associated block and optionally destructuring the expression to bind extracted values to local variables within that block’s lexical scope.

Syntax

if (expression case pattern when condition) {
  // Executed if the expression matches the pattern AND the condition evaluates to true
} else if (expression case pattern) {
  // Chained pattern matching
} else {
  // Executed if no patterns match
}

Core Mechanics

1. Expression Evaluation and Matching The statement begins by evaluating the target expression. The result is then tested against the pattern following the case keyword. If the structure, type, or value of the expression satisfies the pattern’s rules, the match is considered successful. 2. Variable Binding and Destructuring Patterns can extract data from the matched expression. When a pattern includes variable declarations (using var, final, or explicit types), a successful match binds the corresponding sub-components of the expression to those variables.
final dynamic record = ('Dart', 3.0);

// Destructures the record and binds 'name' and 'version'
if (record case (String name, double version)) {
  print(name);    // Scope of 'name' is restricted to this block
  print(version); // Scope of 'version' is restricted to this block
}
3. Guard Clauses (when) An if-case statement can include an optional when clause to evaluate an arbitrary boolean condition alongside the pattern match. The when condition is evaluated only if the initial pattern match succeeds, and it can reference variables bound during the destructuring phase. If the when condition evaluates to false, the entire if-case branch fails.
if (record case (String name, double version) when version >= 3.0) {
  // Executed only if the pattern matches AND the version is >= 3.0
}

Supported Pattern Types in If-Case

The pattern in an if-case statement supports Dart’s full pattern matching specification, including:
  • Constant Patterns: Matches exact values.
if (value case 42) { ... }
  • Relational Patterns: Uses operators (<, >, <=, >=, ==, !=) to match against a value.
if (value case >= 10) { ... }
  • Logical Patterns: Combines patterns using && (AND) or || (OR).
if (value case >= 0 && <= 100) { ... }
  • Variable Patterns: Matches a value and binds it to a new variable, optionally enforcing a type check.
if (value case String s) { ... } // Matches if value is a String, binds to 's'
  • Collection Patterns: Matches the structure and elements of Lists or Maps.
if (json case {'status': 200, 'data': var payload}) { ... }
  • Object Patterns: Matches against the properties of a class instance using getters.
if (exception case FormatException(message: var msg)) { ... }
  • Wildcard Patterns: Matches a type or value without binding it to a variable, using the _ identifier.
if (value case int _) { ... } // Matches any integer
Master Dart with Deep Grasping Methodology!Learn More