Variables and Keywords in Dart

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.

Variable Declaration:

To declare a variable: 

Syntax: type variable_name;

To declare multiple variables of same type: 

Syntax: 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 : 



  1. Variable name or identifiers can’t be the keyword.
  2. Variable name or identifiers can contain alphabets and numbers.
  3. Variable name or identifiers can’t contain spaces and special characters, except the underscore(_) and the dollar($) sign.
  4. 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

filter_none

edit
close

play_arrow

link
brightness_4
code

void main() {
  // Declaring and initialising a variable
  int gfg1 = 10;
   
  // Declaring another variable
  double gfg2;
  bool gfg3;
    
  // Declaring multiple variable
  String gfg4, 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
}

chevron_right


 

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. 

 



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

filter_none

edit
close

play_arrow

link
brightness_4
code

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);
}

chevron_right


Output:

Geeks For Geeks
3.14157

Note: If we use var instead of dynamic in the above code then it will show error.

Output:

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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);
}

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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);
}

chevron_right


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:2:
Error: Can't assign to the const variable 'geek1'.
 geek1 = "Geeks For Geeks Again!!";
 ^^^^^
Error: Compilation failed.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.