Home Dart Dart Programming Language – Variables in Dart

Dart Programming Language – Variables in Dart

5
Dart Programming Language - Variables in Dart
Dart Programming Language - Variables in Dart

In any programming, A variable is “a named space in the memory” that we use to store some values. In other words, it acts a box or a container filled with some values in it. Variable names are also called identifiers, all dart data types Variables in Dart are bit same as that of javascript language.

Here are some Rules we need to follow while creating an identifier/dart variables.

  •  A Variable should not be keywords
  • Variable can contain alphabet’s and numbers.
  • A Variable cannot contain any space or special character, except underscore( _ ) and Dollar symbol ( $ )
  • Variable name should not begin with a number.
  • Note: All the data types in dart are Objects. It means everything in Dart is objects and it inherits object properties. This means the default value of all these data types is null.

Syntax of Variable in dart 

Dart makes use of the var keyword to declare the same. The syntax for declaring a dart variable is as given below:-

var name;

When we declare a variable without giving it any value, we essentially have created a dynamic type of variables, which means that variable can store any kind of values whether it may be an int or a string.

Eg: dart variable declared as a string

var Name = 'proto coders point';

Here is the explanation.

  • var is the Data Type
  • Name is the Variable name
  • Proto Coders Point is the Value stored in the variable

In the above-given example, we are declaring a variable name with the value ” proto coders point “ which means that this variable can only store Strings. when we set any value in single or double inverted commas it will automatically initialize itself a string, in other cases number can directly set to a variable. as shown below:-

void main() {
  
 var number = 123;
  print(number);
 
}

output

123

Dart Programming Language Different data-types variable.

Here is an example where are will implement all kinds of data types variables in dart programming language.

void main() {
     var x = 'Maths pi'; // string variable
     var y = 3.14;       // int variable

     print(x); // Maths pi
     print(y); // 3.14
}

Output

Maths pi
3.14

Example 2: In Dart, we can store different kinds of data in the same variable and change it during run time. Here is an example of flutter math.pi

//this will work know more about this below
void main() {
    var x ;
    x = 'Math pi';
    print(x); // Maths pi

    x = 3.14;
    print(x); // 3.14
}

Output

Math pi

3.14

This is valid Example   because we are not initilizating any value to var x  so we can easily change its data type at runtime.

Example 3

In this Example 3 we will try to mix both example 1 &2.
Now I am going to initialize the variable at the time if declaration and trying to store different data type in the same variable. I Now This will sure give me some error because this is not valid in Dart, as it is valid in javascript.

// This is invalid Example this will work
void main() {

     var x = 'Math pi';
     print(x);  // Maths pi


     x = 3.14;  // Compilation failed error because
                // you are trying to assign double to String variable.
     print(x);
}

Here in above Example 3 we will get compilation Failed Error Because we are trying to assign double to String Variable which is not possible in Dart.

dynamic type keyword variable in dart

There is the dynamic keyword for variable declaration in Dart which is equivalent to JavaScript var keyword.

If we had used dynamic instead of var in Dart code we will not got any error because in dart  programming language dynamic is same as  Var in JavaScript.

dynamic name = 'My Name Is Proto'; // dynamic keyword 
dynamic id = 101; 

name= 123; // dynamic variable can also even store numbers

As in above lines of code we have declared a dynamic variable which is currently storing string values, but as code reach line num 3  it will change its datatype to integer.

if you need the flexibility of a dynamic language. The dynamic type itself is static, but can contain any date type at runtime. Of course, that removes many of the benefits of a type-safe language for that variable.

Example of Dynamic Variable in dart :

void main() {
  
  dynamic n ='Its a Dynamic Value : String';
  
  print(n);
  
  n = 5;
  
  print("its a Dynamic Value : Integer = $n ");
  
  
  
}

OutPut

Its a Dynamic Value : String
its a Dynamic Value : Integer = 5

Final and Const

The final and const keyword are used to declare constants. Here Dart Programming Language allow prevents modifying the values of a variable declared using the final or const keyword.

here final and const keyword variable will work into similar way.

Syntax of const keyword:

const variable_name = value; //if value is string it should have inverted commans

Syntax of final keyword:

final variable_name = value; // is value is string it should have inverted commas.

Here final or const  variable in dart must be initialized at the time of declaration.

Example of final  or const  constants in dart programming language.

void main() {
  
  final abc='xyz';
  print("final data :  "+abc);
  
  const xyz=123;
  print("const data : $xyz");
  
  
}

output

final variable : xyz

const variable : 123

let us check if final or const  data can  be changed or no

void main() {
  
  final abc='xyz';
  print("final variable :  "+abc);
  
  abc="abc"; // this will give me error becz it final which iss constant in nature
  
  const xyz=123;
  print("const variable : $xyz");
  
  
}

// above Code will give me compilation error because once a datatype is  declared as final or const with cannot we changed during runtime.

All in one Dart Programming Language Data type Variables Example

void main() {
  
  String name = 'Smith'; //string data type 
  
  print("Name : "+name);
  
  int num = 10; //integer datatype
  
  print(" Number : $num");

  
  dynamic x = "tom"; //is initialized as string  but later we can change it to double or integer
  
  print("dynamic string : "+x); // dynamic print string
  
  x=123; //dynamic datatype value string gets changed into integer
  
  print("dynamic integer : $x");
  
  final abc='xyz';
  print("final constants"+abc);
  
  const number=123;
  print("constants number : $number");
 
  
  
}

Here name is normal string datatype , num is normal integer datatype , x is a dynamic datatype which can even change its datatype at runtime, final  abc is a constants datatype string which cannot be changed at runtime.

To run the above Dart Programs online visit here https://dartpad.dartlang.org/

5 Comments