
final
and final
are keywords applied to variables.
Dart
and Flutter
provide constant values assigned to variables using final and const keywords.
const variables know the value at compile time. final variables know the value at Run time.
letās see the sample example of usage of this.
const date=ā2021-01-01ā // compile time constants final date=CalculateDateFunction();// Runtime time constants
Letās see more comparisons with examples
Compile-time vs runtime constants
Both const
and final
values are assigned only once.
compile-time constants are constant values during compile time
Letās see a compile-time constants
void main() {
const number1;
number1=45;
print(number1);
}
The above program throws compile-time error as given below
Error: The const variable ānumber1ā must be initialized.
const number1;
^^^^^^^
Error: Canāt assign to the const variable ānumber1ā.
number1=45;
Letās see an example of Runtime constants also called final
variables.
Declare a variable with the final
keyword. And it is assigned a value of 45.
This compiles fine as the 45 value is assigned at runtime.
The below program compiles and runs output 45.
void main() {
final number1;
number1=45;
print(number1); //45
}
The above These programs, the Difference is
const
is compile-time constants andfinal
is runtime constants.- const value is initialized at only compile-time, Runtime time assigning is not possible
- Both are assigned with values only one-time assignment
Both final and const variables are not reassigned for variables
Once variables are declared and initialized with their value, You can reassign these variables with new values.
Reassigning const variable:
void main() {
const number1 = 123;
print(number1);
number1 = 45;
}
It throws the error Constant variables canāt be assigned a value. Reassigning final variable
void main() {
final number1 = 123;
print(number1);
number1 = 45;
}
It throws error The final variable ānumber1ā can only be set once.
From the above programs, Both types are not reassignable once the value is assigned to variables.
Class level member constants
Letās see how class-level member constants can be declared. variables declared in the class are called member variables.
Class member constants const:
class Student {
const id = 5;
}
It throws Error: Only static fields can be declared as const.
Class level constants are always declared with the static
keyword.
below are valid member variable constants
class Student {
static const id = 5;
}
Class member constants must be initialized during declaration.
class Student {
static const id;
}
The above program throws Error: The const variable āidā must be initialized.
The constant variable must be initialized on the same line.
Class member constants final:
The final member variables are declared with the final
keyword.
valid final class variable example
class Employee {
final id = 15;
}
class final member variables should be initialized on the same line.
The below program throws an Error: Final field āidā is not initialized.
class Employee {
final id ;
}
valid final variables in class
class Employee {
final id=4 ;
}
Difference between final and const variables in Dart and Flutter
Following are comparisons of final and const variable declarations in dart and flutter
Final variable | Const variable |
---|---|
applied to variables | variables |
assigned value only once | assigned value only once |
Runtime constants | Compile-time constants |
Class variables constants | |
declared with const | Const must be used with |
static keyword to make it constant | |
Class constants are initialized with value on the same line | Class constants are initialized with value on the same line |