Dart Tutorial

Dart Tutorial Single-Page Application Architecture Dart Features Dart Installation Guide Dart Basic Program Dart Syntax Dart Keywords Dart Variables Dart Comments Dart Standard Input Output Dart Important Concepts

Data Types

Built-in Data Types Numbers Strings Booleans Lists Sets Maps Runes and Graphemes Symbols Enumerations Constants Queues

Other data types

Objects Future and stream Iterable Miscellaneous types

OPERATORS

Precedence and associativity Arithmetic operators Equality and Relational operators Type Test Operators Assignment Operators Logical Operators Bitwise and Shift Operators Miscellaneous operators

Control Flow Statements

Introduction If statement If-else statement If-else-if statement Loops Switch and case Dart Break And Continue Assert In Dart

FUNCTIONS

Dart function Types of Functions Anonymous function main( ) function Lexical scope and closure Recursion Common Collection Methods

Object Oriented Concepts

Dart Object-Oriented Concepts Dart Classes Dart Constructors Dart This Keyword Dart Super Keyword Static Members Method Overriding Dart Interfaces Inheritance Dart Abstract Classes Dart Builder Classes Dart Callable Classes

Dart Type System

Dart Type System Dart Soundness Dart Type Inference

MISCELLANEOUS

Dart Isolates Dart Typedef Dart Metadata Dart Packages Dart Generics Dart Generators Dart Concurrency Dart Unit Testing Dart Html Dom Dart URIs Dart Extends, With and Implements Keywords Dart Optional Parameters Rust Vs Dart C++ vs Dart Golang Vs Dart Dart Basics Exception Handling

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.