> ## 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 If-Null

The `??` operator, formally known as the **if-null operator**, is a binary, short-circuiting operator that evaluates and returns its left operand if it is not `null`; otherwise, it evaluates and returns its right operand.

## Syntax

```dart theme={"dark"}
expression1 ?? expression2
```

## Evaluation Mechanics

The operator follows a strict, lazy evaluation sequence:

1. `expression1` (the left operand) is evaluated.
2. If the value of `expression1` is non-null, the operator returns that value. `expression2` (the right operand) is **short-circuited** and never evaluated.
3. If the value of `expression1` is `null`, `expression2` is evaluated, and its resulting value is returned.

```dart theme={"dark"}
int? leftOperand = null;
int rightOperand = 10;

// Evaluates to 10 because leftOperand is null.
int result = leftOperand ?? rightOperand; 
```

## Short-Circuiting Behavior

Because the operator short-circuits, any side effects or computationally expensive operations placed on the right-hand side will only execute if the left-hand side resolves to `null`.

```dart theme={"dark"}
int expensiveComputation() {
  // Simulating an expensive task
  return 100;
}

int? value = 5;

// The function expensiveComputation() is never invoked 
// because 'value' is already non-null.
int result = value ?? expensiveComputation();
```

## Type Resolution

Dart's sound null safety system applies specific static typing rules to the `??` operator. The static type of the expression `e1 ?? e2` is determined by the least upper bound of the non-nullable type of `e1` and the type of `e2`.

Consequently, the resulting expression is non-nullable *only if* the right operand (`e2`) is non-nullable. If the right operand is a nullable type, the resulting expression remains nullable.

```dart theme={"dark"}
String? nullableString = null;
String nonNullableString = "Fallback";
String? anotherNullable = null;

// The compiler infers 'resolvedString' as the non-nullable type 'String'
// because the right operand is non-nullable.
String resolvedString = nullableString ?? nonNullableString;

// The compiler infers 'stillNullable' as the nullable type 'String?'
// because the right operand is nullable.
String? stillNullable = nullableString ?? anotherNullable;
```

## Operator Precedence

The `??` operator has lower precedence than the logical OR operator (`||`) and higher precedence than the conditional ternary operator (`?:`). This dictates how unparenthesized complex expressions are parsed by the compiler.

```dart theme={"dark"}
bool? condition = null;
bool fallbackA = false;
bool fallbackB = true;

// '||' has higher precedence than '??'. 
// Parsed as: condition ?? (fallbackA || fallbackB)
bool result1 = condition ?? fallbackA || fallbackB;

// '??' has higher precedence than '?:'.
// Parsed as: (condition ?? fallbackB) ? "Yes" : "No"
String result2 = condition ?? fallbackB ? "Yes" : "No";
```

## Operator Chaining

The `??` operator is left-associative and can be chained sequentially. A chained expression evaluates strictly from left to right, terminating and returning the first non-null value it encounters.

```dart theme={"dark"}
int? a = null;
int? b = null;
int c = 42;

// Evaluates 'a', then 'b', then 'c'. Returns 42.
int result = a ?? b ?? c;
```

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