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

A constant variable in Dart is an immutable reference whose value cannot be reassigned after its initial assignment. Dart enforces immutability through two distinct keywords: `final` for runtime evaluation and `const` for compile-time evaluation.

## The `final` Keyword

A `final` variable can be set only once. Its value is determined at runtime, meaning it can be initialized using the result of a function call, a constructor, or any expression evaluated during program execution. Once initialized, the reference cannot point to a different object.

```dart theme={"dark"}
final String language = 'Dart';
final DateTime initializationTime = DateTime.now(); // Valid: Evaluated at runtime

// language = 'Flutter'; // ERROR: The final variable 'language' can only be set once.
```

## The `const` Keyword

A `const` variable is a strict compile-time constant. Its value must be entirely known, fixed, and evaluated during compilation. All `const` variables are implicitly `final`.

```dart theme={"dark"}
const double pi = 3.14159;
const int maxRetries = 5;
const int total = maxRetries * 2; // Valid: Math operations on constants are compile-time constants

// const DateTime time = DateTime.now(); // ERROR: Method invocation is not a constant expression.
```

## Class-Level Constants

The application of `final` and `const` differs within class structures. Instance variables can be `final` (initialized via constructors), but they cannot be `const`. To define a compile-time constant associated with a class, you must use the `static const` modifiers.

```dart theme={"dark"}
class NetworkConfig {
  final String baseUrl; // Instance-level runtime constant
  
  static const int timeoutMilliseconds = 5000; // Class-level compile-time constant

  NetworkConfig(this.baseUrl);
}
```

## Deep vs. Shallow Immutability

The choice between `final` and `const` dictates the depth of immutability for collections and objects.

* **`final` provides shallow immutability:** It prevents the variable from being reassigned to a new memory address, but the internal state of the referenced object can still be mutated.
* **`const` provides deep, transitive immutability:** It freezes the entire object graph. Neither the reference nor the internal state of the object can be altered.

```dart theme={"dark"}
// final: Reference is immutable, but the object is mutable
final List<int> finalList = [1, 2, 3];
finalList.add(4); // Valid: Modifies the internal state of the list
// finalList = [5, 6]; // ERROR: Cannot reassign a final variable

// const: Both reference and object are deeply immutable
const List<int> constList = [1, 2, 3];
// constList.add(4); // RUNTIME ERROR: Cannot add to an unmodifiable list
// constList = [5, 6]; // ERROR: Cannot reassign a const variable
```

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