> ## 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.

# Dart Conditional

The `?:` operator, formally known as the conditional operator or ternary operator, is an expression-based control flow construct in Dart. It evaluates a boolean condition and returns the result of one of two subsequent expressions based on that boolean value, effectively acting as a single-line, expression-level `if-else` statement.

```text theme={"dark"}
condition ? consequentExpression : alternativeExpression
```

**Mechanics and Evaluation**

1. The `condition` is evaluated first. In Dart's sound null safety system, this expression must strictly evaluate to a type of `bool`.
2. If the `condition` evaluates to `true`, the `consequentExpression` is evaluated, and its result becomes the value of the entire ternary operation.
3. If the `condition` evaluates to `false`, the `alternativeExpression` is evaluated, and its result becomes the value of the entire ternary operation.

**Short-Circuit Behavior**
The operator employs short-circuit evaluation. Dart guarantees that only one of the two result expressions is evaluated at runtime. The expression not selected by the condition is completely ignored, preventing any side effects (such as function calls or variable mutations) associated with the unselected branch from executing.

**Type Resolution**
The static return type of the `?:` operation is determined by the least upper bound (LUB) of the static types of the `consequentExpression` and the `alternativeExpression`.

```dart theme={"dark"}
void main() {
  int x = 5;
  int y = 10;
  // Both expressions are of type 'int'. The resulting type is 'int'.
  int result = (x > y) ? x : y; 
  print(result); // Outputs: 10

  bool isInteger = true;
  // Expressions are 'int' and 'double'. The resulting type is 'num' (the LUB).
  num numericResult = isInteger ? 10 : 10.5; 
  print(numericResult); // Outputs: 10
}
```

**Associativity and Evaluation Order**
The conditional operator is right-associative in its syntax parsing. When multiple `?:` operators are chained without explicit parentheses, the syntax tree groups them from right to left. However, **runtime evaluation strictly occurs left-to-right**. The leftmost condition is evaluated first, and short-circuiting dictates whether the nested rightward expressions are evaluated at all.

```text theme={"dark"}
// Syntax structure of chained ternary operators
condition1 ? expression1 : condition2 ? expression2 : expression3

// Implicit right-to-left grouping (parsing)
condition1 ? expression1 : (condition2 ? expression2 : expression3)
```

**Cascade Operator Restriction**
In Dart's formal grammar, the branches of the conditional operator expect an `expressionWithoutCascade`. Consequently, using an unparenthesized cascade operator (`..` or `?...`) inside a ternary operation results in a syntax error. To use a cascade within the `consequentExpression` or `alternativeExpression`, the cascade operation must be explicitly wrapped in parentheses.

```dart theme={"dark"}
class Builder {
  int value = 0;
  void increment() {
    value++;
  }
}

void main() {
  bool condition = true;
  Builder obj = Builder();
  Builder alternative = Builder();

  // Syntax Error: The parser expects an expressionWithoutCascade
  // var errorResult = condition ? obj..increment() : alternative;

  // Correct: Wrapping the cascade in parentheses resolves the grammar conflict
  var result = condition ? (obj..increment()) : alternative;
  
  print(result.value); // Outputs: 1
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Dart Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
