A variable in Dart is used to store data that can be accessed and modified during program execution. Each variable has a name and a data type that defines the kind of value it can store.
Example: The following example shows how to declare and use variables of different data types in Dart.
void main() {
int num = 10;
double price = 0.2;
bool isValid = false;
String str1 = "0", str2 = "Geeks for Geeks";
print(num);
print(price);
print(isValid);
print(str1);
print(str2);
}
Output
10
0.2
false
0
Geeks for Geeks
Syntax
To Declare a Variable:
data_type variable_name;
To Declare Multiple Variables:
data_type variable1, variable2, variable3;
Types of Variables
Dart supports different types of variables:
- Static variables
- Dynamic variables
- final variables
- const variables
Rules for Naming Variables
- Variable names cannot be Dart keywords.
- Variable names can contain letters, numbers, _ and $.
- Variable names cannot contain spaces or special characters.
- Variable names cannot start with a number.
Note: Dart is a type-safe language, meaning it checks whether the stored value matches the variable's data type.
Dynamic Variables
A variable declared with the dynamic keyword can store values of different data types during program execution.
Syntax:
dynamic variable_name;
Example: The following example shows how a dynamic variable can change its data type.
void main() {
dynamic value = "Geeks For Geeks";
print(value);
value = 3.14157;
print(value);
}
Output
Geeks For Geeks
3.14157
Explanation:
- dynamic allows the variable to store values of different types.
- The variable first stores a String value and later stores a double value.
- If var is used instead of dynamic, changing the data type later will produce an error.
Final And Const Keyword
final and const are used to create variables whose values cannot be changed after assignment.
- final variables are assigned once at runtime.
- const variables are compile-time constants and must be assigned a value during declaration.
1. Final: A final variable can only be assigned once.
Syntax:
final variable_name;
final data_type variable_name;
Below is the implementation of final keywords in Dart Program:
void main() {
final name = "Geeks For Geeks";
final String msg = "Learning Dart";
print(name);
print(msg);
}
Output
Geeks For Geeks
Learning Dart
Explanation:
- final variables cannot be reassigned after initialization.
- The datatype can be written explicitly or inferred automatically.
2. Const: A const variable stores a compile-time constant value.
Syntax:
const variable_name;
const data_type variable_name;
Note: const variables must be initialized at the time of declaration.
Below is the implementation of const Keyword in Dart Program:
void main() {
const appName = "Geeks For Geeks";
const String version = "1.0";
print(appName);
print(version);
}
Output
Geeks For Geeks
1.0
Explanation:
- const variables are fixed at compile time.
- Their values cannot be changed later in the program.
Null Safety
Dart supports null safety, which means variables cannot store null unless they are explicitly declared as nullable. This helps prevent unexpected null-related errors in programs.
Example: Assigning null to a Non-Nullable Variable
void main() {
int a = 10;
a = null;
print(a);
}
Output
Error: A value of type 'Null' can't be assigned to a variable of type 'int'.
Explanation: The variable a is of type int, so it cannot store null.
Declaring Nullable Variables
To allow a variable to store null, add ? after the data type.
void main() {
int? a;
a = null;
print(a);
}
Output
null
Explanation: int? makes the variable nullable, so it can store both integer values and null.