Dart Constants

In this tutorial you will learn about the Dart Constants and its application with practical example.

Dart Constants refers to an immutable values. Constants are basically literals whose values cannot be modified or changed during the execution of program. Constant must be initialized when declared as values cannot be assigned it to later.

How to Define Constants In Dart

In Dart, constants can be created in following two ways –

  • Using final keyword.
  • Using const keyword.

In Dart, final and const keyword are used to declare constants. Dart prevents to change the values of a variable declared using the final or const keyword. The final or const keywords can be used in conjunction with data type. A final variable can be set only once while const keyword represents a compile-time constant. The constant declared with const keyword are implicitly final.

Define Constants Using final Keyword

Syntax:-

Or

Example:-

Output:-

Define Constants Using const Keyword

Syntax:-

Or

Example:-

Output:-

Note :- Dart throws an exception if an attempt is made to modify variables declared with the final or const keyword.

Recommended:-  Dart Hello World Program
In this tutorial we have learn about the Dart Constants and its application with practical example. I hope you will like this tutorial.