Lets start by defining what a variable is, a variable is a named space that stores values in memory.
Syntax
A variable must be declared before it is used. Dart uses the var keyword to achieve the same.
var name = 'levis';
All variables in dart store a reference to the value. Uninitialized variables have an initial value of null. This is because Dart considers all values as objects. Look at the example below.
void main(){
String name;
print (name);
}
output
null
Dynamic keyword
Variables declared without a static type can be referred as dynamic. Variables can be also declared using the dynamic keyword in place of the var keyword. Example
void main() {
dynamic y = "levis";
print(y);
}
Final and const
The final and const keyword are used to declare constants. In Dart, values of a variable declared using the final or const keyword can not be modified. Variables declared using the const keyword are implicitly final
Syntax
Final keyword
final variable_name
Const keyword
const variable_name
Example of final keyword code
void main() {
final num1 = 50;
print(num1);
}
Output
50
Example – const keyword
void main() {
const pi = 3.14;
const area = pi*5*12;
print("The output is ${area}");
}
The above example declares two constants, pi and area, using the const keyword hence area variable’s value is a compile-time constant.
Output
The output is 188.4
Note : Final and const variables can’t be modified.