Sub topic 2
Variables, final, const, and built-in types
Assume you know basic programming, can use a terminal and editor, and understand classes and objects. This chapter focuses on how Dart stores values and controls whether those values can change.
Choosing a declaration
A variable is a named reference to a value. Dart lets you declare variables with var, final, or const.
- Use
varwhen the variable will be assigned a new value. - Use
finalwhen the variable is assigned once at runtime. - Use
constwhen the value is known at compile time.
Dart infers the type when you use var, final, or const. You can also write the type explicitly.
void main() {
final name = 'Dart';
var version = 3;
const pi = 3.14159;
version = version + 1;
print('Hello, $name $version ${pi * 2}');
}Here, name cannot be assigned again because it is final. The value of version can change because it is declared with var. The value of pi is a compile-time constant.
final does not make an object deeply immutable. It prevents reassignment of the reference, but the referenced object may still change.
void main() {
final numbers = <int>[1, 2];
numbers.add(3); // Allowed.
// numbers = [4, 5]; // Compile-time error.
print(numbers);
}Compile-time constants
A const value must be constructed from values the compiler already knows. A runtime result cannot be used as a const value.
void main() {
const width = 10;
const height = 20;
const area = width * height;
final currentTime = DateTime.now();
print(area);
print(currentTime);
}currentTime must be final because the current time is available only while the program runs. A const object can be reused by the runtime, which is useful for fixed configuration and immutable values.
Built-in types
Dart's common built-in types include:
| Type | Represents | Example |
|---|---|---|
| `int` | Whole numbers | `42` |
| `double` | Floating-point numbers | `3.14` |
| `num` | Either an `int` or `double` | `10.5` |
| `bool` | `true` or `false` | `isReady` |
| `String` | Text | `'hello'` |
| `List<T>` | Ordered values | `<String>['a', 'b']` |
| `Set<T>` | Unique values | `<int>{1, 2}` |
| `Map<K, V>` | Key-value pairs | `<String, int>{'Ada': 36}` |
num is useful when a function accepts either kind of number. Use int or double when the operation requires a specific representation. Collection type arguments, such as String in List<String>, describe the elements allowed in that collection.
Type inference and common errors
Dart checks types before the program runs. Once Dart infers a variable's type, assigning an incompatible value produces an error.
void main() {
var count = 3;
count = 4;
// count = 'four'; // Error: count is an int.
}You can use Object for a value of any non-nullable object type, but you must inspect or cast it before using type-specific members. Avoid dynamic unless you intentionally want to disable much of Dart's static checking.
Exercise
Which declaration should you use for a value returned by DateTime.now() that will not be reassigned?
Show answer
Use final, because the value is produced at runtime and the binding does not change.