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

A `final` variable in Dart is a single-assignment variable. Once a `final` variable is initialized, its memory reference is locked and cannot be reassigned to a new value. Unlike compile-time constants (`const`), `final` variables are evaluated at runtime. Top-level and `static` `final` variables are lazily initialized upon their first read, whereas instance variables are evaluated at instance creation.

## Syntax and Type Inference

You can declare a `final` variable with an explicit type or rely on Dart's type inference. If the type is omitted, the analyzer infers it from the assigned value.

```dart theme={"dark"}
final String explicitString = 'Dart';
final inferredString = 'Dart'; // Inferred as String
```

## Local Scope Assignment Rules

A `final` variable must be assigned exactly once before it is read. Within a local scope (such as a function body), a `final` variable can be declared uninitialized (a "blank final") as long as the compiler can guarantee it is assigned before its first use.

```dart theme={"dark"}
void main() {
  final int status;

  // Valid: First and only assignment within local scope
  status = 200; 

  // Compile-time error: The final variable 'status' can only be set once.
  // status = 404; 
}
```

## Late Initialization

Due to Dart's sound null safety, top-level and instance `final` variables must typically be initialized at declaration or during object construction. To declare an uninitialized top-level or instance `final` variable that will be assigned exactly once at a later point, you must use the `late final` modifier.

```dart theme={"dark"}
late final String configuration;

void loadConfig() {
  // Valid: First and only assignment of a late final variable
  configuration = 'Loaded'; 
  
  // Error: Late final variable 'configuration' has already been initialized.
  // configuration = 'Reloaded'; 
}
```

## Runtime Evaluation

Because `final` variables are evaluated at runtime, they can be assigned values derived from function calls, object instantiations, or other runtime calculations.

```dart theme={"dark"}
// Valid: DateTime.now() is executed at runtime
final DateTime initializationTime = DateTime.now(); 
```

## Shallow Immutability

The `final` keyword enforces *reference immutability*, not *object immutability*. If a `final` variable holds a reference to a mutable object (like a `List` or `Map`), the internal state of that object can still be modified. The restriction only prevents the variable identifier from pointing to a completely different object in memory.

```dart theme={"dark"}
final List<int> sequence = [1, 2, 3];

// Valid: Mutating the internal state of the referenced object
sequence.add(4); 

// Compile-time error: Attempting to reassign the reference to a new List
// sequence = [10, 20]; 
```

## Class Instance Variables

When used as instance variables within a class, `final` fields must be initialized before the constructor body executes (unless marked `late`). This can be done at the point of declaration, via initializing formal parameters, or within the constructor's initializer list.

```dart theme={"dark"}
class NetworkResponse {
  final int statusCode;
  final String body;
  final DateTime timestamp = DateTime.now(); // Initialized at declaration

  // Initialized via formal parameters
  NetworkResponse(this.statusCode, this.body);
  
  // Alternatively, initialized via the initializer list
  NetworkResponse.error(this.statusCode) : body = 'Error occurred';
}
```

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