Open In App

Dart – Const And Final Keyword

Last Updated : 02 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Dart supports the assignment of constant value to a variable. These are done by the use of the following keywords:

  • const keyword
  • final keyword


These keywords are used to keep the value of a variable static throughout the code base, meaning once the variable is defined its state cannot be altered. There are no limitations if these keywords have a defined data type or not.

final Keyword In Dart

The final keyword is used to hardcode the values of the variable, and it cannot be altered in the future; neither can any kind of operations performed on these variables alter its value (state).

// Without datatype
final variable_name;

// With datatype
final data_type variable_name;

Example :

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

Note : If we do not assign a value to a final variable and tries to use it, reassigning it will result in an error as below.

Error 1 : Using an uninitialized final variable

Error: Final variable ‘geek1’ must be assigned before it can be used.

print(geek1);


Error 2 : Attempting to reassign a final variable

Can’t assign to the final variable ‘geek1’.

geek1 = “Geeks For Geeks Again!!”;


const Keyword in Dart

The Const keyword in Dart behaves similarly to the final keyword. The only difference between final and const is that the const makes the variable constant from compile-time only. Using const on an object makes the object’s entire deep state strictly fixed at compile-time, and the object with this state will be considered frozen and completely immutable.

Example :

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 String geek2 = "Geeks For Geeks Again!!";
    
    // Printing variable geek2
    print(geek2);
}

 
Output:

Geeks For Geeks
Geeks For Geeks Again!!

 

Assigning value without const keyword and then by const keyword

Without Const Keyword

Dart
// Declaring a function
gfg() => [1, 2];

// Main function
void main() {
    // Assigning value
    // through function
    var geek1 = gfg();
    var geek2 = gfg();
    
    // Printing result
    // false
    print(geek1 == geek2);
    print(geek1);
    print(geek2);
}


Output :

false
[1, 2]
[1, 2]


With Const Keyword

Dart
// Declaring a function
gfg() => const[1, 2]; 

// Main function
void main() { 
    // Assigning value
    // through function
    var geek1 = gfg(); 
    var geek2 = gfg(); 
    
    // Printing result
    // true 
    print(geek1 == geek2);
    print(geek1);
    print(geek2);
}

 

Output :

true
[1, 2]
[1, 2]


Const Keyword Properties:

  1. It is necessary to create them from the data available during the compile time. For instance: setting string “GeeksForGeeks” is fine but setting the current time is not.
  2. They are deeply and transitively immutable.
  3. They are canonicalised.

Note : If we do not assign a value to a const variable and tries to use it or not , reassigning it will result in an error as below. 

Error 1 : Using an Uninitialized const Variable

Error: The const variable ‘geek1’ must be initialized.
Try adding an initializer (‘= expression’) to the declaration.
const geek1 ;

Error 2 : Attempting to Reassign a const Variable

Error: Can’t assign to the const variable ‘geek1’.
geek1 = “Geeks For Geeks Again!!”;




Next Article
Article Tags :

Similar Reads