> ## 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 Cast Pattern

A cast pattern in Dart allows you to assert the type of a matched value during pattern matching and destructuring. It uses the `as` keyword to perform a downcast, throwing a `TypeError` at runtime if the matched value does not conform to the specified type, rather than silently failing the match.

## Syntax

```dart theme={"dark"}
<pattern> as <type>
```

The cast pattern consists of an inner pattern followed by the `as` keyword and a type annotation.

## Mechanics

During runtime execution, a cast pattern evaluates an incoming value through the following strict sequence:

1. **Casting:** The Dart runtime applies the `as <type>` operation to the incoming value first. If the value is not of the specified `<type>`, a `TypeError` is thrown immediately, halting the pattern match.
2. **Matching:** If the cast succeeds, the runtime then evaluates the newly cast value against the inner `<pattern>`.
3. **Binding:** If the inner pattern is a variable pattern, the variable is bound with the static type defined by `<type>`.

Because it utilizes Dart's standard `as` operator, a cast pattern does not alter the control flow by rejecting a match if the type is incorrect. Instead, an invalid type assertion results in an unhandled runtime exception.

## Syntax Visualization

**In Variable Declarations (Destructuring):**

```dart theme={"dark"}
// Casts the first element to an int, then binds it to 'a'.
// Throws a TypeError if the element is not an int.
final [a as int, b] = someList; 
```

**In Switch Cases:**

```dart theme={"dark"}
switch (dynamicValue) {
  // Casts dynamicValue to String, then matches against 'var x'.
  // Throws if dynamicValue is not a String.
  case var x as String:
    print(x.length); // x is statically typed as String
    break;
}
```

**Nested within Object Patterns:**

```dart theme={"dark"}
// Destructures a record, casting the 'value' field to a double before binding to 'v'.
final (id: _, value: v as double) = someRecord;
```

## Cast Pattern vs. Safe Type Matching

It is critical to distinguish the cast pattern (`as`) from patterns that safely test types without throwing exceptions, such as typed wildcard patterns or typed variable patterns.

* **Typed Wildcard Pattern (`<type> _`):** Acts as a guard. If the value is not of the specified type, the pattern match fails, and execution proceeds to the next case. It tests the type without binding the value to a variable.
* **Typed Variable Pattern (`<type> <name>`):** Declares a variable with a type annotation. It also acts as a guard, failing gracefully if the type does not match, but binds the value to a variable if successful.
* **Cast Pattern (`<pattern> as <type>`):** Acts as an assertion. It assumes the match is valid and forces the type evaluation first. If the value is not of the specified type, the program throws a `TypeError`.

*Note: Never use a bare type literal (e.g., `case String:`) to test a type. This creates a **Constant Pattern** that matches the `Type` object itself, not instances of that type.*

```dart theme={"dark"}
switch (dynamicValue) {
  // Typed Wildcard Pattern: Fails gracefully, moves to next case. Does not bind.
  case String _:
    break;

  // Typed Variable Pattern: Fails gracefully, moves to next case. Binds to 's'.
  case String s:
    break;

  // Cast Pattern: Throws TypeError if not a String. Binds to 's'.
  case var s as String: 
    break;
}
```

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