Dart – Variables
Variables in Dart:
A variable name is the name assign to the memory location where the user stores the data and that data can be fetched when required with the help of the variable by calling its variable name. There are various types of variable which are used to store the data. The type which will be used to store data depends upon the type of data to be stored.
Syntax: To declare a variable:
type variable_name;
Syntax: To declare multiple variables of same type:
type variable1_name, variable2_name, variable3_name, ....variableN_name;
Type of the variable can be among:
1. Integer 2. Double 3. String 4. Booleans 5. Lists 6. Maps
Conditions to write variable name or identifiers are as follows:
- Variable name or identifiers can’t be the keyword.
- Variable name or identifiers can contain alphabets and numbers.
- Variable name or identifiers can’t contain spaces and special characters, except the underscore(_) and the dollar($) sign.
- Variable name or identifiers can’t begin with number.
Note: Dart supports type-checking, it means that it checks whether the data type and the data that variable holds are specific to that data or not.
Example 1:
Printing default and assigned values in Dart of variables of different data types.
Dart
void main() { // Declaring and initialising a variable int gfg1 = 10; // Declaring another variable double gfg2 = 0.2; // must declare double a value or it // will throw error bool gfg3 = false ; // must declare boolean a value or it // will throw error // Declaring multiple variable String gfg4 = "0" , gfg5 = "Geeks for Geeks" ; // Printing values of all the variables print(gfg1); // Print 10 print(gfg2); // Print default double value print(gfg3); // Print default string value print(gfg4); // Print default bool value print(gfg5); // Print Geeks for Geeks } |
Output:
10 null null null Geeks for Geeks
Keywords in Dart:
Keywords are the set of reserved words which can’t be used as a variable name or identifier because they are standard identifiers whose function are predefined in Dart.

Keywords in Dart
Dynamic type variable in Dart:
This is a special variable initialised with keyword dynamic. The variable declared with this data type can store implicitly any value during running the program. It is quite similar to var datatype in Dart, but the difference between them is the moment you assign the data to variable with var keyword it is replaced with the appropriate data type.
Syntax:
dynamic variable_name;
Example 2:
Showing how datatype are dynamically change using dynamic keyword.
Dart
void main() { // Assigning value to geek variable dynamic geek = "Geeks For Geeks" ; // Printing variable geek print(geek); // Reassigning the data to variable and printing it geek = 3.14157 ; print(geek); } |
Output:
Geeks For Geeks 3.14157
Note: If we use var instead of dynamic in the above code then it will show error.
Error compiling to JavaScript: main.dart:9:10: Error: A value of type 'double' can't be assigned to a variable of type 'String'. geek = 3.14157; ^ Error: Compilation failed.
Final And Const Keyword in Dart:
These keywords are used to define constant variable in Dart i.e. once a variable is defined using these keyword then its value can’t be changed in the entire code. These keyword can be used with or without data type name.
Syntax for Final: // Without datatype final variable_name // With datatype final data_type variable_name
Syntax for Const: // Without datatype const variable_name // With datatype const data_type variable_name
Example 3:
Using final keywords in a Dart program.
Dart
void main() { // Assigning value to geek1 variable without datatype final geek1 = "Geeks For Geeks" ; // Printing variable geek1 print(geek1); // Assigning value to geek2 variable with datatype final String geek2 = "Geeks For Geeks Again!!" ; // Printing variable geek2 print(geek2); } |
Output:
Geeks For Geeks Geeks For Geeks Again!!
Now, if we try to reassign the geek1 variable in the above program, then:
Output:
Error compiling to JavaScript: main.dart:8:3: Error: Can't assign to the final variable 'geek1'. geek1 = "Geeks For Geeks Again!!"; ^^^^^ Error: Compilation failed.
Example 4:
Using const keywords in a Dart program.
Dart
void main() { // Assigning value to geek1 variable without datatype const geek1 = "Geeks For Geeks" ; // Printing variable geek1 print(geek1); // Assigning value to geek2 variable with datatype const geek2 = "Geeks For Geeks Again!!" ; // Printing variable geek2 print(geek2); } |
Output:
Geeks For Geeks Geeks For Geeks Again!!
Now, if we try to reassign the geek1 variable in the above program, then output is as follows:
Error compiling to JavaScript: main.dart:8:2: Error: Can't assign to the const variable 'geek1'. geek1 = "Geeks For Geeks Again!!"; ^^^^^ Error: Compilation failed.
Please Login to comment...