Summary: in this tutorial, you’ll learn how to define constants using the Dart const
keyword.
Introduction to the Dart const keyword
The following program converts a distance from mile to kilometer:
void main() {
double distanceMile = 1;
double distanceKm = distanceMile * 1.609;
print('$distanceMile Mile = $distanceKm Km');
}
Code language: Dart (dart)
Output:
1.0 Mile = 1.609 Km
Code language: Dart (dart)
How it works.
First, declare a variable distanceMile
that stores the distance in miles and initializes its value to 1:
double distanceMile = 1;
Code language: Dart (dart)
Second, declare a variable distanceKm
and initialize its values to an expression that converts mile to kilometer:
double distanceKm = distanceMile * 1.609;
Code language: Dart (dart)
Third, display the result of the conversion:
print('$distanceMile Mile = $distanceKm Km');
Code language: Dart (dart)
The program multiplies the distance in miles with the number 1.609 to get the distance in kilometers.
If a program has many places that convert mile to kilometer, you’ll use the fixed number 1.609 across the program.
The issue arises when you want to make it more accurate by changing the value from 1.609
to 1.609344
. In this case, you need to change the value in multiple places.
Also, when looking at this fixed number in the future, you may not fully understand its meaning. As the result, the code becomes more difficult to maintain.
To fix this, you may define a variable that stores the number 1.609
and use that variable whenever you need to convert a distance from miles to kilometers. For example:
void main() {
double distanceMile = 1;
double toKm = 1.609;
double distanceKm = distanceMile * toKm;
print('$distanceMile Mile = $distanceKm Km');
}
Code language: Dart (dart)
The problem with this approach is that the toKm
variable can be changed accidentally for example to zero. This results in incorrect conversions.
To fix it permanently, you can use constants. A constant is an identifier whose value doesn’t change. To define a constant, you add the const
keyword like this:
const type constantName = value;
Code language: Dart (dart)
Since Dart can infer the type of the constant from the value, you can omit its type like this:
const constantName = value;
Code language: Dart (dart)
Since the value of a constant is not changed, you need to initialize its value to a constant immediately when you define it.
For example, the following changes the toKm
to a constant:
void main() {
double distanceMile = 1;
const toKm = 1.609;
double distanceKm = distanceMile * toKm;
print('$distanceMile Mile = $distanceKm Km');
}
Code language: Dart (dart)
Once a constant is defined, its value cannot be changed. If you attempt to do so, you’ll get an error. For example:
void main() {
double distanceMile = 1;
const toKm = 1.609;
double distanceKm = distanceMile * toKm;
print('$distanceMile Mile = $distanceKm Km');
// error
toKm = 1.6;
}
Code language: Dart (dart)
It’s important to notice that a constant only accepts a value that is truly constant at compile time. For example, the following will result in an error:
void main() {
const currentTime = DateTime.now();
print(currentTime);
}
Code language: Dart (dart)
In this example, the DateTime.now()
returns the current time.
The program attempts to define a constant and assigns the current time returned by the DateTime.now()
to the currentTime
constant.
However, the value of the DateTime.now()
can be determined only at runtime, not compile time. Therefore, the Dart compiler issues an error.
To fix this, you need to declare the variable as final
like this:
void main() {
final currentTime = DateTime.now();
print(currentTime);
}
Code language: Dart (dart)
And you’ll learn about the final
variables in the next tutorial.
Summary
- A constant is an identifier whose value doesn’t change.
- Use the
const
keyword to define a constant.