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

 Difference between the const and final keyword in Dart and Flutter

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;

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.

The below program compiles and runs output 45.

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

The above 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;
  number1 = 45;

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

void main() {
  final number1 = 123;
  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 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 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: 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 variableConst variable
applied to variablesvariables
assigned value only onceassigned value only once
Runtime constantsCompile-time constants
Class variables constants
declared with constConst must be used with
static keyword to make it constant
Class constants are initialized with value on the same lineClass constants are initialized with value on the same line

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
You'll get a notification every time a post gets published here.

Related posts

How to convert Double to Integer or Integer to double in Dart| Flutter By Example

Perl How to: Find a Given Variable String is numeric or not

Dart/Flutter: Check if String is Empty, Null, or Blank example

How to generate Unique Id UUID in Dart or Flutter Programming| Dart or Flutterby Example

How to Sort List of numbers or String in ascending and descending in Dart or Flutter example

Dart Enum comparison operator| Enum compareByIndex example| Flutter By Example

Dart Example - Program to check Leap year or not