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.

A switch statement is a branching control flow construct that evaluates a given expression against a series of case clauses. When a match is found, the statement executes the associated block of code. In Dart 3.0 and later, switch statements fully support pattern matching, allowing for complex destructuring, logical operators, and guard clauses.
switch (expression) {
  case pattern1:
    // Statements executed if expression matches pattern1
  case pattern2 when condition:
    // Statements executed if expression matches pattern2 AND condition is true
  default:
    // Statements executed if no patterns match
}

Execution Mechanics and Rules

  • Evaluation: The target expression is evaluated exactly once.
  • Matching: Cases are evaluated sequentially from top to bottom. Dart utilizes pattern matching semantics to determine a match. If no patterns match, the default clause (or the wildcard pattern case _:) is executed.
  • Implicit Break (Dart 3.0+): break statements are no longer required at the end of a case block. Dart automatically breaks out of the switch statement after executing a matched, non-empty case.
  • Empty Case Fallthrough: Implicit fallthrough is strictly limited to empty case clauses. If a case has no statements, execution falls through to the next case.
switch (command) {
  case 'OPEN':
  case 'START': // Implicit fallthrough allowed because 'OPEN' is empty
    executeStart();
  case 'CLOSE':
    executeClose(); // Implicit break occurs here
}

Explicit Fallthrough (Labeled Cases)

To achieve fallthrough across non-empty cases, Dart requires explicit control flow using the continue keyword combined with a label.
switch (status) {
  case 'PENDING':
    print('Status is pending.');
    continue processLabel; // Explicitly jumps to processLabel
    
  processLabel:
  case 'PROCESSING':
    print('Status is processing.');
}

Pattern Matching and Logical Operators

Dart allows the use of logical operators (||, &&) and relational operators directly within case clauses to match multiple conditions without relying on empty case fallthrough.
switch (numericValue) {
  case 1 || 2 || 3:
    print('Matches 1, 2, or 3');
  case > 10 && < 20:
    print('Matches between 11 and 19');
}

Guard Clauses

A case can be appended with a when keyword followed by a boolean expression. This is known as a guard clause. The case will only match if both the pattern matches the expression and the guard clause evaluates to true.
switch (point) {
  case (int x, int y) when x == y:
    print('Coordinates are identical.');
  case (int x, int y):
    print('Coordinates are different.');
}

Exhaustiveness Checking

When switching over algebraic data types—such as enum types, sealed classes, or bool—the Dart analyzer enforces exhaustiveness. The compiler will throw an error if the switch statement does not account for every possible value or subtype, unless a default or wildcard (_) clause is provided.
sealed class Shape {}
class Circle extends Shape {}
class Square extends Shape {}

// The compiler enforces that both Circle and Square are handled.
switch (shape) {
  case Circle():
    // Handle circle
  case Square():
    // Handle square
}
Master Dart with Deep Grasping Methodology!Learn More