What is the difference between const and final keyword in Dart?
const means its initial value is must be fixed, can not be a dynamic value;
final means its initial value is must be fixed but can be a dynamic value, equal to the var with a fixed value.
void main() {
const sum = 1 + 2;
// const can not change its value
print("sum = ${sum}");
// Const variables must be initialized with a constant value.
const time = new DateTime.now();
// Error: New expression is not a constant expression.
print("time = ${time}");
}
// new DateTime.now();
// dynamic timestamp
void main() {
final sum = 1 + 2;
// final can not change its value
print("sum = ${sum}");
final time = new DateTime.now();
// final === var with fixed value
print("time = ${time}");
}
https://dart.dev/guides/language/language-tour#final-and-const