TExcelTAD
Home › Dart › Dart Variables

Dart Variables

A variable is a named reference to a value. Dart gives you four keywords that control how a variable may be assigned: var, final, const and late. Choosing the right one documents your intent and lets the compiler catch mistakes.

Declaring variables

dart · try it yourself
void main() {
  var city = 'Da Nang';      // type inferred as String
  String country = 'Vietnam'; // type written explicitly
  int population = 1220000;

  city = 'Hoi An';           // var can be reassigned
  print('$city, $country');
  print(population);
}
Hoi An, Vietnam
1220000

var does not mean "any type". Once inferred, the type is fixed:

dart · try it yourself
void main() {
  var count = 3;
  count = 'three';
}
example.dart:3:11: Error: A value of type 'String' can't be assigned to a variable of type 'int'.
  count = 'three';
          ^
Error: AOT compilation failed
Bad state: Generating AOT kernel dill failed!

The four keywords

Keyword Reassignable? Value known at Typical use
var / a type Yes Runtime Counters, accumulators, loop state
final No (set once) Runtime Most local variables, fields set in a constructor
const No Compile time Literals, configuration, immutable collections
late Depends (late or late final) Later, before first read Lazy values, fields initialised after construction

final vs const

Both prevent reassignment, but const goes further: the value must be computable at compile time, and it is deeply immutable and canonicalised (identical constants share one object).

dart · try it yourself
void main() {
  final now = DateTime(2025, 3, 14); // computed at runtime: fine for final
  const maxUsers = 100;               // compile-time constant
  const tax = maxUsers * 0.1;         // constants can use other constants

  print(now.year);
  print(tax);

  final list = [1, 2];
  list.add(3);              // final only locks the *variable*, not the object
  print(list);

  const frozen = [1, 2];
  try {
    frozen.add(3);          // a const list is deeply immutable
  } catch (e) {
    print('Error: $e');
  }

  print(identical(const [1, 2], const [1, 2]));
}
2025
10.0
[1, 2, 3]
Error: Unsupported operation: Cannot add to an unmodifiable list
true

Tip: Prefer final for locals you never reassign — it's the most common choice in idiomatic Dart. Use const for values that are truly fixed, especially in Flutter widget trees where it avoids rebuilding.

late variables

late tells the compiler: "this non-nullable variable will be assigned before it is read — trust me". It has two uses.

1. Delayed initialisation. The value is assigned later, but the type stays non-nullable:

dart · try it yourself
late String description;

void setup() {
  description = 'Configured at startup';
}

void main() {
  setup();
  print(description);
}
Configured at startup

If you read a late variable before assigning it, you get a LateInitializationError at runtime — so use it only when you are sure.

2. Lazy initialisation. A late variable with an initialiser runs that initialiser only on first access:

dart · try it yourself
int expensive() {
  print('computing...');
  return 42;
}

void main() {
  late final answer = expensive();
  print('before first use');
  print(answer);
  print(answer); // not computed again
}
before first use
computing...
42
42

Default values and dynamic

Non-nullable local variables have no default value — you must assign them before use. Nullable variables (int?) start as null.

dynamic switches off static checking for a variable. It accepts anything, and mistakes surface only at runtime. Object? also accepts anything but keeps type checks; prefer it.

dart · try it yourself
void main() {
  int? maybe;
  print(maybe);

  dynamic anything = 5;
  anything = 'now a string';
  print(anything.length);

  Object? safer = 'text';
  if (safer is String) print(safer.toUpperCase());
}
null
12
TEXT

Test yourself

Which keyword requires a value that is known at compile time?

  1. final
  2. var
  3. late
  4. const

What happens with final list = [1]; list.add(2);?

  1. Compile error
  2. Runtime error
  3. It works — final only prevents reassigning the variable
  4. The list is copied

When does the initialiser of late final x = compute(); run?

  1. When the program starts
  2. When the declaration is reached
  3. The first time x is read
  4. Never