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.
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.
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 |
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
finalfor local variables you don't reassign, andconstfor compile-time constants. In Flutter,constwidgets are created once and reused, which makes rebuilds cheaper — you'll seeconsteverywhere.
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.
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.
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,iforvarcannot be used as names.
Test yourself
What happens when you write var x = 5; x = 'five';?
- x becomes the string 'five'
- A compile error, because x is an int
- x becomes dynamic
- A runtime warning only
What is the difference between final and const?
- There is none
constcan be reassignedfinalis set once at runtime;constmust be known at compile timefinalonly works for strings
When is the initializer of late final int x = f(); evaluated?
- When the program starts
- When x is first read
- Never
- At compile time