Dart Programming – Variables

  • Post author:
  • Post category:Dart
  • Post comments:1 Comment
dart programming variables

In this guide, we will discuss Dart Programming Variables. A variable is “a named space in the memory” that stores values. In other words, it acts a container for values in a program. Variable names are called identifiers. Following are the naming rules for an identifier −

  • Identifiers cannot be keywords.
  • Identifiers can contain alphabets and numbers.
  • Identifiers cannot contain spaces and special characters, except the underscore (_) and the dollar ($) sign.
  • Variable names cannot begin with a number.

Type Syntax

A variable must be declared before it is used. Dart uses the var keyword to achieve the same. The syntax for declaring a variable is as given below −

var name = 'Smith';

All variables in dart store a reference to the value rather than containing the value. The variable called name contains a reference to a String object with a value of “Smith”.

type syntax

Dart supports type-checking by prefixing the variable name with the data type. Type-checking ensures that a variable holds only data specific to a data type. The syntax for the same is given below −

String name = 'Smith'; 
int num = 10;

Consider the following example −

void main() { 
   String name = 1; 
}

The above snippet will result in a warning since the value assigned to the variable doesn’t match the variable’s data type.

Output

Warning: A value of type 'String' cannot be assigned to a variable of type 'int' 

All uninitialized variables have an initial value of null. This is because Dart considers all values as objects. The following example illustrates the same −

void main() { 
   int num; 
   print(num); 
}

Output

Null 

The dynamic keyword

Variables declared without a static type are implicitly declared as dynamic. Variables can be also declared using the dynamic keyword in place of the var keyword.

The following example illustrates the same.

void main() { 
   dynamic x = "tom"; 
   print(x);  
}

Output

tom

Final and Const

The final and const keyword are used to declare constants. Dart prevents modifying the values of a variable declared using the final or const keyword. These keywords can be used in conjunction with the variable’s data type or instead of the var keyword.

The const keyword is used to represent a compile-time constant. Variables declared using the const keyword are implicitly final.

Syntax: final Keyword

final variable_name

OR

final data_type  variable_name

Syntax: const Keyword

const variable_name

OR

const data_type variable_name

Example – final Keyword

void main() { 
   final val1 = 12; 
   print(val1); 
}

Output

12

Example – const Keyword

void main() { 
   const pi = 3.14; 
   const area = pi*12*12; 
   print("The output is ${area}"); 
}

The above example declares two constants, pi and area, using the const keyword. The area variable’s value is a compile-time constant.

Output

The output is 452.15999999999997

Note − Only const variables can be used to compute a compile time constant. Compile-time constants are constants whose values will be determined at compile time

Example

Dart throws an exception if an attempt is made to modify variables declared with the final or const keyword. The example given below illustrates the same −

void main() { 
   final v1 = 12; 
   const v2 = 13; 
   v2 = 12; 
}

The code given above will throw the following error as output −

Unhandled exception: 
cannot assign to final variable 'v2='.  
NoSuchMethodError: cannot assign to final variable 'v2=' 
#0  NoSuchMethodError._throwNew (dart:core-patch/errors_patch.dart:178) 
#1      main (file: Test.dart:5:3) 
#2    _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:261) 
#3    _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:148)

Next Topic : Click Here

This Post Has One Comment

Leave a Reply