> ## 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 Final Field

A final field in Dart is a class-level variable declared with the `final` keyword, enforcing strict single-assignment semantics. Once a final field is initialized, its memory reference is locked and cannot be reassigned. At compile time, Dart implicitly generates a getter for a final field but omits the setter, rendering the field read-only after instantiation.

## Initialization Mechanics

Dart requires that **all** final fields (both nullable and non-nullable) be explicitly initialized before the constructor body executes. A nullable final field (e.g., `final int? x;`) does not implicitly default to `null` to satisfy the `final` assignment contract; it will throw a compile-time error if left uninitialized. Initialization must be achieved through one of three primary mechanisms:

**1. Inline Initialization**
The field is assigned a value directly at the point of declaration.

```dart theme={"dark"}
class Configuration {
  final String environment = 'production';
}
```

**2. Initializing Formals**
The field is initialized via the constructor signature using `this.fieldName`. This is syntactic sugar that binds the passed argument directly to the field before the constructor body runs.

```dart theme={"dark"}
class Point {
  final int x;
  final int y;

  Point(this.x, this.y);
}
```

**3. Initializer Lists**
The field is assigned a value in the constructor's initializer list, which executes after arguments are evaluated but before the constructor body. This is required when the initialization requires computation or when mapping constructor parameters to fields with different names. Because an initializing formal (`this.radius`) cannot be accessed within the initializer list, a standard parameter must be used to perform computations.

```dart theme={"dark"}
class Circle {
  final double radius;
  final double area;

  Circle(double radius) : this.radius = radius, area = 3.14159 * radius * radius;
}
```

## Shallow Immutability

The `final` keyword enforces *shallow immutability*. It protects the variable's reference, not the underlying object's state. If a final field holds a reference to a mutable object (such as a `List`, `Map`, or a custom class instance), the internal state of that object can still be modified.

```dart theme={"dark"}
class DataContainer {
  final List<String> items = [];

  void mutate() {
    // Valid: Mutating the state of the referenced object
    items.add('new item'); 
    
    // Invalid: Reassigning the reference causes a compile-time error
    // items = ['another', 'list']; 
  }
}
```

## `late final` Fields

By combining the `late` and `final` modifiers, the initialization of a final field can be deferred until runtime, bypassing the strict requirement to initialize it during construction. A `late final` field retains single-assignment semantics; attempting to assign a value to it a second time will throw a late initialization error (a subclass of `Error`) at runtime.

```dart theme={"dark"}
class Database {
  late final String connectionString;

  void initialize(String url) {
    // Valid on the first call. 
    // Throws a runtime error if called again.
    connectionString = url; 
  }
}
```

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