×

Dart Constants

Dart Constants are the objects or variables whose values can’t change or modify during the execution of the program. Their use case is when we want a particular value to remain unchanged throughout the program’s execution. 

Defining Constants in Dart

In Dart, variables can be assigned with constant values using the following two methods : 

  • Using the ‘ final ’ keyword = final variables can be set only once as they are assigned the value at the compile-time debarring any changes in the future. ‘ final ’ keyword is used with the variable name to assign constant value.

Implementation =   final var_name = value ;

                                                 Or

                                 final data_type var_name = value ;

The second one is used when data type has to be explicitly mentioned, along with the variable name. 

  • Using the ‘ const ’ keyword = const is a compile-time constant and is implicitly equivalent to final. ‘ const ’ keyword is used with the variable name to assign constant value.

Implementation =  const var_name = value; 

                                               Or

                                       const data_type var_name = value; 

The second one is used when data type has to be explicitly mentioned, along with the variable name. 

In any case, if we try to change the value of constants, the Dart compiler throws an exception. 

Program

void main( )

{

  // normal variable declaration

  var a = 5 ;

  // printing the value of a

  print( ' \n Value of a is : $a ' ) ;

  // changing the value of a

  a = 6 ;

  // printing the value of a

  print( ' \n Value of a is : $a ' ) ;

  // variable declaration using final keyword

  final b = 1 ;

  // printing the value of final variable b

  print( ' \n Value of b is : $b ' ) ;

  // variable declaration using const keyword

  const c = 2 ;

  // printing the value of constant variable c

  print( ' \n Value of c is : $c ' ) ;

}

Output :

Value of a is : 5

Value of a is : 6

Value of b is : 1

Value of c is : 2

Program

void main( )

{

  // normal variable declaration

  var a = 5  ;

  // printing the value of a

  print( ' \n Value of a is : $a ' )  ;

  // changing the value of a

  a = 6 ;

  // printing the value of a

  print( ' \n Value of a is : $a ' ) ;

  // variable declaration using final keyword

  final b = 1 ;

    b = 7 ;

  // variable declaration using cosnt keyword

  const c = 2 ;

    c = 8 ;

  }

Output :

Dart Constants

Clearly, the compiler throws an exception since we tried to change the value of b and c which are constants.


Related Topics

Dart Maps

Map is an object-based data structure that associates keys and values that can be of any data type. Every key can occur only once, while the values can be used multiple...

5 minutes read.

Dart Loops

Looping refers to repeating or reusing the same lines of code repeatedly until a particular test condition evaluates to true. It is also known as iterating.  It is effectively used when...

4 minutes read.

Dart Generators

Synchronous Generator Dart Generator is a unique function that allows us to generate price sequences. Generators return values when needed; means that the value is generated if we try to duplicate...

5 minutes read.

Dart Static Members

Static Members in Dart Static members are the members of the class declared using the 'static' keyword. the static members have the following characteristics :  All the objects of the class having...

3 minutes read.

Abstract Classes in Dart

Abstract classes in Dart are those classes that only contain abstract methods  (methods that do not contain any implementation). These classes are specifically designed for the purpose of inheritance. We...

4 minutes read.

Common Collection Methods

Most of the programming languages have arrays as a way to list items together. However, Dart has a collection of data structures similar to array. These are supported by the...

3 minutes read.

Golang vs Dart

Go is the procedural programming language. It was founded in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson at Google but was launched in 2009 as the language of...

2 minutes read.

Dart Logical Operators

Logical operators are the operators that combine two or more conditions, and accordingly return a Boolean value : true or false. Assume the value of variable A is 25 and B...

2 minutes read.

Typedef in Dart

In Dart, typedef is used to generate a function type that we can use as a type annotation for declaring variables and return types of the function type. An alias...

4 minutes read.

Dart Construtors

Constructors are the very important concept in any programming language. They are the special functions created in the classes to allocate memory to the objects of that class when created...

4 minutes read.

Dart Strings

Strings data type in Dart A Dart string is a sequence of characters with an encoding of UTF-16. The text associated with string data type is enclosed withing single (‘  ’)...

3 minutes read.

Dart Runes and Graphemes

Runes and Graphemes data types in Dart Runes and Graphemes Runes are the special string of Unicode UTF-32 bits that represent the special syntax. Unicode defines a unique numeric value for each...

2 minutes read.

Dart Recursion

Recursion is one of the most important and interesting concepts in any programming language. It can be defined as a process in which a function calls itself directly or indirectly....

5 minutes read.

Dart Booleans

Booleans Dart has a data type named ‘bool’ to represent Boolean values : true and false, which are compile-time constants. ‘True’ or ‘False’ cannot be assigned with values 1 or 0. Example, bool...

1 minute read.

Dart If-else statement

In the previous section, we studied about if statements in detail. If – block is executed when the condition evaluates to true, else if the condition evaluates to false, then...

1 minute read.

Dart Miscellaneous types

Some Other Data types in Dart Apart from the data types studied so far, we have three other data types also which are as follows: NeverDynamicVoid Let us understand each one in detail, Never...

3 minutes read.

Dart Standard Input & Output

Standard Input in Dart (stdin): The standard input stream reads data both synchronously and asynchronously from the keyboard.  In Dart programming language, .readLineSync( ) function is used to accept input from the...

3 minutes read.

Interfaces in Dart

An interface in Dart refers to the syntax or blueprint that any class must adhere to. It basically defines the array of methods available on the object. It provides the...

3 minutes read.

Dart Classes

Dart is an object – oriented programming language that supports all the object-oriented programming concepts such as classes, objects, inheritance, data abstraction and data encapsulation. A class can be defined as...

8 minutes read.

Callable Classes in Dart

Just like the functions, we can also call the instances of classes in Dart. Such classes are also known as “callable ” classes. We need to use the “call( )”...

3 minutes read.