Dart final and const
Dart final
Sometimes a value should never change after you set it, like a birth year or a user ID.
For this, use the final keyword instead of var:
Result:
1995
A final variable can be set only once.
If you try to give it a new value, the program does not run:
Example
void main() {
final birthYear = 1995;
birthYear = 2000;
print(birthYear);
}
Try it Yourself »
Result:
main.dart:3:3: Error: Can't assign to the final variable 'birthYear'.
birthYear = 2000;
^^^^^^^^^
You can also write the type after final:
Example
void main() {
final String country = 'Norway';
final int population = 5500000;
print(country);
print(population);
}
Try it Yourself »
Result:
Norway
5500000
Dart const
The const keyword also creates a variable that cannot change:
Example
void main() {
const pi = 3.14159;
const daysInWeek = 7;
print(pi);
print(daysInWeek);
}
Try it Yourself »
Result:
3.14159
7
The difference is when the value is known.
A const value must be a compile-time constant.
This means that Dart must know the value before the program starts, just by reading the code.
Numbers, strings, true, false, and calculations with other constants are compile-time constants:
Example
void main() {
const minutesPerHour = 60;
const hoursPerDay = 24;
const minutesPerDay = minutesPerHour * hoursPerDay;
print(minutesPerDay);
}
Try it Yourself »
Result:
1440
final vs const
A final value can be found while the program runs.
For example, the current time is only known when the program runs, so it can be final:
Result:
true
The example prints true, because the current year is greater than 2000.
The current time cannot be const, because Dart cannot know it before the program starts:
Result:
main.dart:2:24: Error: Cannot invoke a non-'const' constructor where a const expression is expected.
Try using a constructor or factory that is 'const'.
const now = DateTime.now();
^^^
A value from another variable is not a constant either, even if the variable does not change:
Result:
main.dart:3:17: Error: Not a constant expression.
const total = price * 2;
^^^^^
| final | const | |
|---|---|---|
| Can be changed after it is set | No | No |
| Value can come from the running program | Yes | No |
| Value must be known before the program starts | No | Yes |
| Example | final now = DateTime.now(); | const pi = 3.14159; |
const Lists
A list stores many values in one variable, inside square brackets [].
You will learn about lists in the Lists chapter.
A final list cannot be replaced with another list, but you can still change the items inside it:
Example
void main() {
final fruits = ['Apple', 'Banana'];
fruits.add('Cherry');
print(fruits);
}
Try it Yourself »
Result:
[Apple, Banana, Cherry]
A const list is fixed, so you cannot add, remove or change items.
If you try, the program stops with an error when it reaches that line:
Example
void main() {
const fruits = ['Apple', 'Banana'];
print(fruits);
fruits.add('Cherry');
}
Try it Yourself »
Result:
[Apple, Banana]
Unhandled exception:
Unsupported operation: Cannot add to an unmodifiable list
Tip: Use final for values that are set once and never changed.
Use const for fixed values that you can write directly in the code, like pi or a list of weekdays.