> ## 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 Null-Check Pattern

The null-check pattern (`?`) is a refutable pattern matching construct in Dart that matches a value if and only if the runtime value is not `null`, subsequently binding the unwrapped value to a subpattern. It acts as a combined type promotion and destructuring mechanism, extracting non-nullable types from nullable expressions.

## Syntax

The null-check pattern is denoted by appending a question mark (`?`) directly after a subpattern:

```dart theme={"dark"}
<subpattern>?
```

## Mechanics

When a nullable value of type `T?` is evaluated against a null-check pattern:

1. **Evaluation:** Dart checks the runtime value of the expression.
2. **Rejection:** If the value is `null`, the pattern match fails.
3. **Extraction & Promotion:** If the value is not `null`, Dart unwraps the value, promotes its static type from `T?` to `T`, and passes it to the inner `<subpattern>` for further matching or variable binding.

## Refutable Contexts

Because the null-check pattern can fail (when the value is `null`), it is strictly a **refutable** pattern. It is valid only in refutable contexts, such as `if-case` statements, `switch` statements, and `switch` expressions. In these contexts, a `null` value simply causes the match to fail, allowing execution to fall through to the next case or bypass the conditional block.

```dart theme={"dark"}
String? nullableText = "Dart";

// Using if-case
if (nullableText case var text?) {
  // Match succeeds. 'text' is bound and promoted to non-nullable String.
  print(text.length); 
}

// Using switch statement
switch (nullableText) {
  case String s?: 
    // Matches any non-null String. 's' is non-nullable.
    break;
  case null:
    // Handles the null case explicitly.
    break;
}
```

## Nested Destructuring

The null-check pattern is frequently nested within collection, object, or record patterns to enforce non-nullability on specific elements during destructuring.

```dart theme={"dark"}
(int?, int?) nullableCoordinates = (10, null);

if (nullableCoordinates case (var x?, var y?)) {
  // This match fails because the second element evaluates to null.
  // Both x and y must be non-null for the entire record pattern to match.
}
```

## Irrefutable Contexts (Compile-Time Error)

The null-check pattern **cannot** be used in irrefutable contexts, such as standard variable declarations or assignments, because irrefutable contexts require patterns that are guaranteed to match. Attempting to use a null-check pattern in an irrefutable context results in a compile-time error.

```dart theme={"dark"}
int? maybeNumber = 42;

// COMPILE-TIME ERROR: Refutable patterns can't be used in an irrefutable context.
var (number?) = (maybeNumber); 
```

To achieve destructuring in an irrefutable context where a `null` value should throw a runtime exception, Dart provides the **null-assert pattern** (`!`). The null-assert pattern forces the match and throws if the value is `null`, making it valid for irrefutable declarations:

```dart theme={"dark"}
int? maybeNumber = 42;

// Valid: Uses null-assert (!), not null-check (?). 
// Throws a runtime exception if maybeNumber is null.
var (number!) = (maybeNumber); 
```

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