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

`dynamic` is a special type annotation in Dart that explicitly disables static type checking for a variable, deferring all type resolution and member access validation to runtime. By assigning the `dynamic` type, you instruct the Dart analyzer to assume the variable supports any method or property, effectively opting it out of Dart's sound static type system.

## Core Mechanics

When a variable is declared as `dynamic`, the compiler permits reassignment to values of any type and allows the invocation of any method or property without compile-time errors. If the invoked member does not exist on the object's actual runtime type, the Dart Virtual Machine (VM) throws a `NoSuchMethodError` during execution.

```dart theme={"dark"}
dynamic variable = 'Hello'; // Initially holds a String
variable = 42;              // Reassignment to an int is permitted

// The compiler allows this because static checking is disabled.
// However, it throws a NoSuchMethodError at runtime because 
// the int type does not have a 'substring' method.
variable.substring(1); 
```

## Type System Relationships

To understand `dynamic`, it must be contrasted with other foundational type concepts in Dart:

### `dynamic` vs. `Object`

`Object` (or `Object?`) is the root of the Dart class hierarchy. Both `dynamic` and `Object` can hold any value, but they interact with the compiler differently:

* **`Object`**: Enforces static type checking. You can only invoke methods defined on the `Object` class (e.g., `toString()`, `hashCode`). To use type-specific methods, you must perform a safe cast (using `as` or `is`).
* **`dynamic`**: Bypasses static type checking entirely. The compiler allows any method call without casting.

```dart theme={"dark"}
Object obj = 'Dart';
// obj.length; // COMPILE ERROR: The getter 'length' isn't defined for the class 'Object'.

dynamic dyn = 'Dart';
print(dyn.length); // COMPILES AND RUNS: Outputs 4.
```

### `dynamic` vs. `var`

`var` is a keyword used for variable declaration that triggers **type inference**, whereas `dynamic` is an actual type.

* If you initialize a variable using `var`, the compiler permanently locks the variable to the inferred type.
* If you initialize a variable using `dynamic`, its type remains mutable at runtime.

```dart theme={"dark"}
var inferredString = 'Dart';
// inferredString = 10; // COMPILE ERROR: A value of type 'int' can't be assigned to a variable of type 'String'.

dynamic dynamicVariable = 'Dart';
dynamicVariable = 10;   // COMPILES AND RUNS.
```

## Implicit `dynamic`

Dart will implicitly assign the `dynamic` type in specific scenarios where type information is absent and cannot be inferred:

**1. Uninitialized `var` declarations:**
If a variable is declared with `var` but not initialized, Dart cannot infer its type and defaults to `dynamic`.

```dart theme={"dark"}
var uninitialized; // Implicitly typed as dynamic
uninitialized = 10;
uninitialized = 'String';
```

**2. Omitted Generic Type Arguments:**
When instantiating a generic class (like collections) without specifying the type parameter, Dart defaults the type argument to `dynamic`.

```dart theme={"dark"}
List list = [1, 'two', 3.0]; // Implicitly List<dynamic>
```

## Nullability

In Dart's sound null safety system, `dynamic` is inherently nullable. It behaves equivalently to a nullable type (like `String?`), meaning a `dynamic` variable can hold a `null` value without requiring a `?` suffix.

```dart theme={"dark"}
dynamic value = null; // Valid
```

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