Dart – Const And Final Keyword

Dart provides the user to set the value of a certain variable as constants with the help of two keywords:

  1. const keyword
  2. final keyword

These keywords are used to define a constant variable in Dart i.e. once a variable is defined using these keywords then its value can’t be changed in the entire code. These keywords can be used with or without data type names.

Final Keyword In Dart

This keyword is used to assign the value to a variable only once and fixing it, i.e. it can’t be changed. A final variable or field must have an initializer. Once assigned a value, a final variable’s value cannot be changed.

// Without datatype
final variable_name;

// With datatype
final data_type  variable_name;

Example: Using the 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!!

If we try to reassign the same variable then it will display error.

Const Keyword in Dart

Const keyword in Dart is also used to keep the value of the variable constant i.e. avoid it from changing. The difference between final and const is that the const makes the variable constant from compile-time only. While using const on an object, the object’s entire deep state can be determined entirely at compile-time and that the object will be frozen and completely immutable.

Example: 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!!

Example: Assigning value without const keyword and then by const keyword.

Without Const Keyword

Dart

filter_none

edit
close

play_arrow

link
brightness_4
code

// Declaring a function
gfg() => [1, 2]; 
  
// Main function
void main() { 
  // Assiging value
  // through function
  var geek1 = gfg(); 
  var geek2 = gfg(); 
    
  // Printing result
  // false 
  print(geek1 == geek2); 
  print(geek1);
  print(geek2);
}

chevron_right


 

Output :

false
[1, 2]
[1, 2]

With Const Keyword:

Dart

filter_none

edit
close

play_arrow

link
brightness_4
code

// Declaring a function
gfg() => const[1, 2]; 
  
// Main function
void main() { 
  // Assiging value
  // through function
  var geek1 = gfg(); 
  var geek2 = gfg(); 
    
  // Printing result
  // true 
  print(geek1 == geek2);
  print(geek1);
  print(geek2);
}

chevron_right


Output :

true
[1, 2]
[1, 2]

Const Keyword Properties:

  1. They must be created from the data that is available at compile-time and not from the one we require during runtime. For instance: setting string “GeeksForGeeks” is fine but setting current time is not.
  2. They are deeply, transitively immutable.
  3. They are canonicalized.



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 :

Be the First to upvote.


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