Variables in Dart Programming

Dart ProgrammingServer Side ProgrammingProgramming

Dart being a statically typed language demands that we declare the type of variable that we going to use. In simpler terms, it is necessary that we define what kind of data we are going to store in the variable before making use of it.

Example

Consider the example shown below −

 Live Demo

void main(){
   int collegeId = 1234;    // declaring and assigning a variable
   print(collegeId);        // printing the variable's value

   String myName = "mukul";
   print(myName);
}

In the above example, we declared two variables named 'collegeId' and 'myName' and assigned 1234 and "mukul" as their values respectively. In the next line, we simply print the value that we stored in the two variable.

Output

1234
mukul

It should also be noted that Dart also provides us with a bit of flexible feature where we can declare a variable of any data type with the help of the var keyword. So, in the above example, we declared two variables namely 'collegeId' of type int and 'myName' of type string, we can do that without declaring the types and replacing them with var keyword and the dartAnalyzer will automatically infer the type.

Example

Consider the example shown below −

 Live Demo

void main(){
   var collegeId = 1234;
   print(collegeId);

   var myName = "mukul";
   print(myName);
}

Output

1234
mukul

null value

A variable declared in Dart and hasn't assigned any value will automatically hold a null value. It doesn't matter if we data type of the variable is either a bool or String or if we declared the variable using the var keyword, an unassigned variable in Dart will simply hold the null value.

Example

Consider the example shown below −

 Live Demo

void main(){
   var myVariable;           // a var variable
   int collegeID;           // int
   bool isStudent;          // a bool
   double marksInEnglish;  // a double
   String fullName;        // a string

   print(myVariable);
   print(collegeID);
   print(isStudent);
   print(marksInEnglish);
   print(fullName);
}

Output

null
null
null
null
null

It should also be noted that once we have declared a variable of a certain type, we can't assign any value of another type to it.

Example

Consider the example shown below −

void main(){
   int collegeID = 1234;
   print(collegeID);
   collegeID = "idk";
   print(collegeID);
}

In the above example, we are trying to assign a string value to an int variable, which is completely wrong. The output will be −

Output

Error: A value of type 'String' can't be assigned to a variable of
type 'int'.
   collegeID = "idk";
               ^
Error: Compilation failed.

Even if we declare a variable with the var keyword we still aren't allowed to assign the value of another type to the type that has already inferred.

Example

Consider the example shown below −

 Live Demo

void main(){
   var collegeID = 1234;
   print(collegeID);
   collegeID = "idk";
   print(collegeID);
}

Output

Error: A value of type 'String' can't be assigned to a variable of
type 'int'.
   collegeID = "idk";
               ^
Error: Compilation failed.
raja
Published on 24-May-2021 12:16:40
Advertisements