TExcelTAD
Home › Flutter › Dart Variables

Dart Variables

A variable is a name that refers to a value. Dart is statically typed — every variable has a type that is known at compile time — but thanks to type inference you rarely have to write the type yourself.

Declaring variables with var

Use var when the type is obvious from the initial value. Dart infers it and locks it in.

dart · try it yourself
void main() {
  var name = 'Ada';      // inferred as String
  var age = 36;          // inferred as int
  var height = 1.68;     // inferred as double

  print('$name is $age years old and $height m tall.');

  age = 37;              // OK: still an int
  print('Next year: $age');
}
Ada is 36 years old and 1.68 m tall.
Next year: 37

Because age was inferred as int, writing age = 'old'; later would be a compile error. var is not "any type" — it just means "figure out the type for me".

Explicit types

You can always write the type instead of var. This is common for class fields, function parameters, and when a variable is declared before it gets a value.

dart · try it yourself
void main() {
  String city = 'Hanoi';
  int population = 8000000;
  double area = 3358.6;
  bool isCapital = true;

  print('$city: $population people, $area km², capital: $isCapital');
}
Hanoi: 8000000 people, 3358.6 km², capital: true

final and const

Most values in a well-written program never change after they are set. Dart has two keywords for that:

Keyword Can be reassigned? When is the value fixed? Typical use
var / type Yes — Counters, accumulators
final No At runtime, once Values computed while the program runs
const No At compile time Literal numbers, strings, fixed lists
dart · try it yourself
void main() {
  const pi = 3.14159;           // known before the program runs
  final radius = 2.0;           // set once at runtime
  final area = pi * radius * radius;

  print('Area: $area');

  // pi = 3;      // Error: constant variables can't be assigned
  // radius = 5;  // Error: a final variable can only be set once
}
Area: 12.56636

Tip: Prefer final for local variables you don't reassign, and const for compile-time constants. In Flutter, const widgets are created once and reused, which makes rebuilds cheaper — you'll see const everywhere.

late variables

Sometimes you know a non-nullable variable will be assigned before it is used, but not at the point of declaration. Mark it late. If you read it too early, Dart throws an error at runtime instead of at compile time.

dart · try it yourself
late String greeting;

void main() {
  greeting = 'Good morning';
  print(greeting);

  late final int expensive = computeValue();
  print('Before using expensive');
  print('Value: $expensive');
}

int computeValue() {
  print('Computing...');
  return 42;
}
Good morning
Before using expensive
Computing...
Value: 42

Notice that computeValue() ran only when expensive was first read — a late variable with an initializer is lazily evaluated.

dynamic and Object

If you really need a variable that can hold any type, use Object (any non-null value) or dynamic (turns off static checking). Use dynamic sparingly: the compiler can no longer catch mistakes for you.

dart · try it yourself
void main() {
  Object thing = 'text';
  print(thing);
  thing = 10;
  print(thing);

  dynamic anything = 'hello';
  print(anything.length); // checked only at runtime
}
text
10
5

Naming rules

  • Names may contain letters, digits, _ and $, but cannot start with a digit.
  • Dart style uses lowerCamelCase for variables: userName, itemCount.
  • A leading underscore (_secret) makes a name private to its library (file).
  • Keywords like class, if or var cannot be used as names.

Test yourself

What happens when you write var x = 5; x = 'five';?

  1. x becomes the string 'five'
  2. A compile error, because x is an int
  3. x becomes dynamic
  4. A runtime warning only

What is the difference between final and const?

  1. There is none
  2. const can be reassigned
  3. final is set once at runtime; const must be known at compile time
  4. final only works for strings

When is the initializer of late final int x = f(); evaluated?

  1. When the program starts
  2. When x is first read
  3. Never
  4. At compile time