Flutter/Dart Difference between the const and final keyword in Dart?

The keywords final and const are used to denote variables in Dart and Flutter.

In Dart and Flutter, constant values assigned to variables utilize the final and const keywords. const variables hold values known at compile time, while final variables hold values known at runtime.

Let’s examine a usage example.

const date="2021-01-01" // compile time constants
final date=CalculateDateFunction();// Runtime time constants

Let’s see more comparisons with examples

Compile-time vs. Runtime Constants in dart

Both const and final variables are assigned values only once.

Compile-time constants are known during compilation:

Let’s see a compile-time constants

void main() {
  const number1 = 45;
  print(number1);
}

The above program results in a compile-time error:

Error: The const variable ‘number1’ must be initialized. const number1; ^^^^^^^ Error: Can’t assign to the const variable ‘number1’. number1=45;

On the other hand, runtime constants, also known as final variables, are assigned values at runtime:

Declare a variable with the final keyword. And it is assigned a value of 45. This compiles fine as the 45 value is assigned at runtime.

The below program compiles and runs output 45.

void main() {
  final number1;
  number1=45;
  print(number1); //45
}

The above In these examples, the difference lies in:

  • const being compile-time constants and final being runtime constants.
  • const variables must be initialized at compile-time; runtime assignment is not possible.
  • Both types allow only one-time value assignment.

Both final and const variables are not reassigned for variables

Both final and const variables cannot be reassigned after initialization:

Reassigning const variable:

void main() {
  const number1 = 123;
  print(number1);
  number1 = 45;
}

It throws the error Constant variables can’t be assigned a value. Reassigning final variable

void main() {
  final number1 = 123;
  print(number1);
  number1 = 45;
}

It throws error The final variable ‘number1’ can only be set once.

From the above programs, Once initialized, both types of variables cannot be reassigned.

Class-Level Member Constants

Class-level member constants require the static keyword.

variables declared in the class are called member variables.

Using const in class member constants:

class Student {
     const id = 5;
}

It throws Error: Only static fields can be declared as const.

Class level constants are always declared with the static keyword.

below are valid member variable constants

class Student {
     static const id = 5;
}

Class member constants must be initialized during declaration.

class Student {
  static const id;
}

The above program throws Error: The const variable ‘id’ must be initialized.

The constant variable must be initialized on the same line.

Class member constants final: For final member variables, initialization on the same line is necessary.

valid final class variable example

class Employee {
  final id = 15;
}

class final member variables should be initialized on the same line.

Class member constants must be initialized upon declaration; otherwise, an error Error: Final field ‘id’ is not initialized. is thrown.

class Employee {
  final id ;
}

valid final variables in class

class Employee {
  final id=4 ;
}

Difference between final and const variables in Dart and Flutter

Following are comparisons of final and const variable declarations in dart and flutter

Final variableConst variable
applied to variablesvariables
Value assigned only onceValue assigned only once
Runtime constantsCompile-time constants
Class variables constantsany variables
declared with constConst must be used with final
Requires static keyword to make it constantstatic is not applied
Class constants are initialized with value on the same lineClass constants are initialized with value on the same line