Dart Variables
Dart Variables
A variable is a container that stores a value, like a name or a number.
To create a variable, write var, then the name of the variable, then an equals sign = and the value:
var name = 'John';
After you create a variable, you can use its name to get the value:
Result:
John
30
The equals sign is called the assignment operator, because it gives (assigns) a value to the variable.
Variables With a Type
Every value in Dart has a type.
The type tells Dart what kind of value it is, for example text or a whole number.
Instead of var, you can write the type before the variable name:
Example
void main() {
String name = 'John';
int age = 30;
double height = 1.82;
bool isStudent = false;
print(name);
print(age);
print(height);
print(isStudent);
}
Try it Yourself »
Result:
John
30
1.82
false
Here are the most common types:
Stringstores text, like'Hello'intstores whole numbers, like30or-5doublestores numbers with decimals, like1.82boolstorestrueorfalse
You will learn more about types in the Data Types chapter.
Type Inference
When you use var, Dart looks at the value and finds the type by itself.
This is called type inference.
You can see the type of a value with runtimeType:
Example
void main() {
var car = 'Volvo';
var year = 2024;
var price = 39999.99;
print(car.runtimeType);
print(year.runtimeType);
print(price.runtimeType);
}
Try it Yourself »
Result:
String
int
double
Note: Both styles are correct.
Many Dart developers use var for local variables, because the type is easy to see from the value.
Change the Value
You can give a variable a new value later in the program:
Result:
10
25
Note that you do not write var again when you change the value.
A Variable Cannot Change Type
The new value must have the same type as the old value.
The variable score below is an int, so you cannot put text in it:
Result:
main.dart:3:11: Error: A value of type 'String' can't be assigned to a variable of type 'int'.
score = 'ten';
^
Dart finds this mistake before the program runs, so you can fix it early.
Create Now, Assign Later
You can create a variable with a type and give it a value later:
Result:
Oslo
But you must give the variable a value before you use it.
If you do not, the program does not run:
Result:
main.dart:3:9: Error: Non-nullable variable 'city' must be assigned before it can be used.
print(city);
^^^^
This rule is part of null safety, which you will learn about in the Null Safety chapter.
Variable Names
A variable name is also called an identifier.
These are the rules for variable names in Dart:
- Names can contain letters, digits, underscores
_and dollar signs$ - Names cannot start with a digit
- Names cannot contain spaces
- Names are case-sensitive, so
ageandAgeare two different variables - Names cannot be reserved words, like
var,classorif
Dart developers write variable names in camelCase.
The first word starts with a small letter, and every word after it starts with a capital letter:
Example
void main() {
var firstName = 'Anna';
var totalPrice = 120;
var isLoggedIn = true;
print(firstName);
print(totalPrice);
print(isLoggedIn);
}
Try it Yourself »
Result:
Anna
120
true
A reserved word cannot be used as a name:
Result:
main.dart:2:7: Error: 'class' can't be used as an identifier because it's a keyword.
Try renaming this to be an identifier that isn't a keyword.
var class = 'Math';
^^^^^