Lesson 20 min

Variables — var, final, const, late

00:00 / 00:00

Variables: var, final, const, and late

In programming, a variable is a named storage location in memory that holds a value. You can think of it as a labeled box where you store data that your program can retrieve and manipulate.

In Dart, variable declaration is highly expressive and type-safe, offering several keywords to control mutability and initialization.


Variable Declaration and Naming Rules

When naming variables in Dart, follow these guidelines:

  • camelCase: Use camelCase for variable names (e.g., userAge, totalPrice).
  • Allowed Characters: Names can contain letters, numbers, underscores (_), and dollar signs ($).
  • No Starting Numbers: A variable name cannot start with a number.
  • No Keywords: You cannot use Dart reserved keywords (e.g., class, void, if) as variable names.

The Four Variable Keywords

Dart provides four main ways to declare variables: var, final, const, and late.

1. var

The var keyword declares a variable without explicitly specifying its type. Dart automatically infers the type based on the initial value assigned (Type Inference). Once inferred, the type cannot change, but the value can be reassigned.

dart
void main() { var name = "Alice"; // Inferred as String name = "Bob"; // OK - reassigning value // name = 42; // Compile error: A value of type 'int' can't be assigned to a variable of type 'String'. }

2. final

A final variable can only be set once. It is initialized at runtime (when the code is executed). Once a value is assigned to a final variable, it cannot be changed.

dart
void main() { final currentTime = DateTime.now(); // Initialized at runtime // currentTime = DateTime.now(); // Compile error: The final variable 'currentTime' can only be set once. }

3. const

The const keyword declares a compile-time constant. Unlike final, a const variable must be initialized with a value that is known at compile-time (before the program runs). const variables are implicitly final and deeply immutable.

dart
void main() { const pi = 3.14159; // Known at compile-time // const currentTime = DateTime.now(); // Compile error: Const variables must be initialized with a constant value. // const lists are also immutable const list = [1, 2, 3]; // list.add(4); // Throws an UnsupportedError at runtime }

4. late

The late keyword is used for variables that are non-nullable but are initialized after their declaration. It is also used to lazily initialize a variable (meaning the initialization code runs only when the variable is accessed for the first time).

dart
class Profile { late String description; void init() { description = "Software Developer"; // Initialized later } } void main() { // Lazy initialization example late String heavyData = loadHeavyData(); print("Main started"); print(heavyData); // loadHeavyData() is called now, not when declared } String loadHeavyData() { print("Loading heavy data..."); return "Data loaded"; }

Comparison Table

KeywordReassignable?Evaluation TimeImmutable Value?Use Case
varYesRuntimeNoGeneral mutable variables with type inference
finalNoRuntimeNoRead-only variables whose values are determined at runtime
constNoCompile-timeYes (Deeply)Fixed constants, mathematical values, configuration values
lateYesRuntime (Lazy)DependentDeclaring non-nullable variables initialized later, or optimization

Summary

  • Use var when you need a variable whose value changes over time.
  • Use final for variables that should not change after initialization at runtime.
  • Use const for absolute values known before compiling.
  • Use late to declare variables that will be initialized later, or to defer heavy resource loading.
WhatsApp