Dart Constants
Overview
Dart Constants are immutable objects, which means that they can’t be changed or modified during the execution of the Dart program. Once we initialize the constant with a value, it cannot be reassigned in the program. Dart will throw an exception if we try to modify the constant.
Dart Constants
The Dart constants can be defined using the following keywords:
- final keyword
- const keyword
These keywords can be used with or without a data type name.
final keyword
The final keyword denotes a single-time assignment of the variable. We can define the constant by using the final keyword. The general syntax to define a constant using the final keyword is as follows:
final constant_name;
final data_type constant_name
Example
final int MAX_ELEMENTS = 10;
const keyword
The const keyword represents the compile-time constant. We can define a constant by using the const keyword. The syntax is as follows:
const constant_name
const data_type constant_name
Example
const String bank_name=”Bank of America”;
—
Dart Tutorials
Dart tutorial on this website can be found at:
https://www.testingdocs.com/dart-tutorials-for-beginners/
More information on Dart: