{

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


final and final are keywords applied to variables.

Dart and Flutter provide constant values assigned to variables using final and const keywords.

const variables know the value at compile time. final variables know the value at Run time.

let’s see the sample example of usage of this.

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

Both const and final values are assigned only once. compile-time constants are constant values during compile time

Let’s see a compile-time constants

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

The above program throws compile-time error as given below Error: The const variable ‘number1’ must be initialized.
const number1; ^^^^^^^ Error: Can’t assign to the const variable ‘number1’. number1=45;

Let’s see an example of Runtime constants also called final variables. 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.

Below program compiles and runs, output 45.

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

The above From these programs, the Difference is

  • const is compile-time constants and final is runtime constants.
  • const value is initialized at only compile-time, Runtime time assigning is not possible
  • Both are assigned with values only one-time assignment

Both final and const variables are not reassigned for variables

Once variables are declared and initialized with their value, You can reassign these variables with new values.

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, Both types are not reassignable once the value is assigned to variables.

Class level member constants

Let’s see how class-level member constants can be declared. variables are declared in the class are called member variables.

Class member constants const:

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 is valid member variable constants

class Student {
     static const id = 5;
}

Class member constants must be initialized during declaration.

class Student {
  static const id;
}

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: The final member variables are declared with the final keyword.

valid final class variable example

class Employee {
  final id = 15;
}

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

The below program throws an Error: Final field ‘id’ is not initialized.

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 variable Const variable
applied to variables variables
assigned value only once assigned value only once
Runtime constants Compile-time constants
Class variables constants
declared with const Const must be used with
static keyword to make it constant
Class constants are initialized with value on the same line Class constants are initialized with value on the same line

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.





Related posts

Dart| Flutter How to: Read pubspec.yaml attributes (version) with examples

How to Create and build a Singleton Class Dart or Flutter example| Dart By Example

What is the difference between var and dynamic and final in dart?| Flutter By Example

How to: Check if Phone number is valid or not in Dart| Flutter By Example

Dart| Flutter How to read an image from a disk and resize a file

Dart/Flutter: How to write setter and getter fields or members variables in a class with Example

Dart| Flutter How to get extension name and MIME type of a file with example