Python Variables, Type Inference, final & const
Dart variables can be declared explicitly (`String name`) or inferred statically with `var`. Immutability is enforced with two keywords: `final` (assigned once at runtime) and `const` (evaluated at compile time and deeply immutable). Understanding `final` vs `const` is fundamental to Flutter widget performance.
"`var` is a label on a container whose contents can change; `final` is a locked box sealed once when packed; `const` is a bronze statue cast at the factory before shipping."
Deep Dive: How It Works
`var` Type Inference: Infers the exact static type on initialization (`var x = 10;` is statically typed as `int`).
`final`: A variable that can only be set once. Its value is computed at runtime (e.g. `final now = DateTime.now();`).
`const`: Compile-time constant. Canonicalized in memory (identical `const` objects share the exact same memory address).
Syntax Blueprint
var name = "Alice"; // Inferred static type (String) final runtimeId = getUuid(); // Immutable after runtime assignment const maxRetries = 5; // Compile-time constant (Deeply immutable)
Three declaration styles balancing mutability, runtime evaluation, and compile-time canonicalization.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Trying to assign a runtime function result to `const` (e.g. `const now = DateTime.now();`).Why it happens: `DateTime.now()` cannot be evaluated until the app runs at runtime.
How to fix: Use `final now = DateTime.now();` for runtime initialization.
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Declare final and const Variables
Declare a `const String serverUrl = "https://api.zen.dev";`.
Declare a `final int port = 8080;`.
Print `Server: $serverUrl on port $port`.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.