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 statement in Dart is a synchronous control flow construct that dictates the execution path of a program by evaluating a boolean expression. If the expression resolves to true, the lexically scoped block of code immediately following the condition is executed.

Standard Syntax

The standard if statement can be chained with else if for sequential condition evaluation and terminated with an optional else block as a fallback execution path.
void main() {
  bool isPrimary = false;
  bool isSecondary = true;

  if (isPrimary) {
    print('Primary condition met.');
  } else if (isSecondary) {
    print('Secondary condition met.');
  } else {
    print('Fallback execution path.');
  }
}

Strict Boolean Evaluation

Unlike languages that utilize “truthy” or “falsy” type coercion (such as JavaScript or C), Dart enforces strict static type checking. The condition within the if clause must evaluate explicitly to a bool type. Attempting to evaluate a non-boolean type, such as an integer or a non-null object, results in a compile-time error.
void main() {
  int statusCode = 200;

  // INVALID: if (statusCode) { print('Success'); } // Results in a compile-time error

  // VALID: Explicit boolean evaluation
  if (statusCode == 200) {
    print('Status is OK.');
  }
}

Block Scope and Braces

Variables declared within the curly braces {} of an if, else if, or else block are lexically scoped to that specific block and cannot be accessed externally. Dart permits the omission of curly braces if the execution block consists of a single statement. However, the official Dart style guide strongly recommends using braces for all control flow structures to prevent dangling-else ambiguity and improve maintainability.
void main() {
  bool condition = true;

  void executeStatement() {
    print('Statement executed.');
  }

  // Valid single-line syntax (braces omitted)
  if (condition) executeStatement();

  // Recommended syntax
  if (condition) {
    executeStatement();
  }
}

Pattern Matching (if-case)

Introduced in Dart 3, the if-case statement extends the standard if construct by integrating pattern matching. It evaluates whether a given expression matches a specific pattern, rather than evaluating a boolean expression. If the match is successful, it can simultaneously extract and bind values to local variables scoped to the if block. The if-case construct can also be chained with standard else if and else blocks, and supports guard clauses using the when keyword to apply additional boolean constraints to a matched pattern.
void main() {
  Object responseData = [200, 404];

  // Evaluates if 'responseData' matches a list containing exactly two integers
  if (responseData case [int x, int y]) {
    // 'x' and 'y' are now bound and lexically scoped to this block
    print('Matched integers: $x and $y');
  } else {
    print('Pattern did not match.');
  }
}
Master Dart with Deep Grasping Methodology!Learn More