×

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 - extends, with and implements Keywords

The application development in Dart programming language using the Flutter framework, regularly experience different usage of the implements, extends and with keywords. Dart has full support for inheritance that is...

5 minutes read.

Dart Comments

Comments are the statements that are used to enhance the readability and understandability of the code. The compiler does not execute these lines. It simply ignores these comments when it scans...

2 minutes read.

Dart Features

Dart is a fully-featured, object-oriented modern language with its roots in the programming language ‘Smalltalk’. It has some of its eminent features similar to the languages such as Java, C#...

4 minutes read.

Builder Classes in Dart

Before understanding builder classes, let us understand about Flutter that makes the best use of the builder classes. Flutter is actually not a programming language, it is a software development...

6 minutes read.

Dart Variables

Variables are used to store the value of a particular data type referred to in the whole program by a name or identifier. They refer to a memory location as...

5 minutes read.

Dart Type inference

The task of interpreting the data types of fields, methods, local variables and most generic type arguments is handled by the static analyser. However, when not enough information is provided...

5 minutes read.

Dart Object-Oriented Concepts

Dart is a programming language that supports object-oriented programming. It is focused on the objects that are real-world entities and supports all the concepts of OOPS, such as objects, classes,...

4 minutes read.

Dart Symbols

Symbols data type in Dart A symbol is an object that is the representation of an operator or identifier in Dart. These are compile-time constants.  They are used in APIs that refer...

1 minute read.

Dart Important Concepts

1. Anything placed in a variable is an object, and an object is an instance of a class. Numbers, functions, and null are all objects in Dart, and all these...

3 minutes read.

Dart Operators Precedence and Associativity

Precedence of operators determines the order in which the operators are evaluated if they are grouped together in a sentence. If two operators share an operand then the one with...

5 minutes read.

Lexical Scope and Closure

Lexical Scope : Lexical scope is a very important term in any programming language. It defines the scope of the variable based on the block it is contained inside. According to...

2 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 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 Exception Handling

Exception Class – An exception is any runtime error that leads to abrupt termination of the program. It helps in addressing the error programmatically. It is aimed to be caught...

4 minutes read.

Dart Function

A Dart function is a collective line of code that are targeted to perform a specialised task. Such lines of code (function) can be reused whenever we need to perform...

6 minutes read.

Dart Switch Case Statement

In the case of if-else statements, we prefer not to use the if-else ladder when there are many test conditions to be evaluated. In such a situation, it is preferable...

4 minutes read.

Dart Types of Functions

Functions are the set of statements intended to perform a specific task. They accept some input from the user, perform the specified computation and generate the output. They are very...

4 minutes read.

Dart Keywords

Keywords are the reserved words of a programming language, and they have a special meaning that comes defined by the language. These cannot be used as an identifier, and all...

1 minute read.

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.

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.