Variables in Dart Programming

In any high-level programming language, variables are something called containers for holding computational data. A variable is actually a named space in the memory surface, that name can be used as a reference to store some values in the space. Variables in Dart programming also work in the same manner.

The syntax or rules of declaring variables is not the same in every programming language. It varies from language to language. In dart programming, we can declare/create a variable like the below:

var variableName = variableValue;
/* or */
dataType variablename = variableValue;

/* Example */
var number = 10;
/* or */
int number = 10;

Note: the semicolon (;) is important* after end of each statement.

Variable naming in Dart

While declaring variables in the dart, you have to follow the variable naming rules. Variables or any identical name excluding keyword is called identifiers. So, it also can be called identifier naming rules. The naming rules of an identifier in dart:

  • Identifiers can contain (0-8), (a-z) and (A-Z) characters.
  • But it can not be a reserved keyword of Dart programming
  • We saw, numbers are allowed, but one exception is variable name can not begin with a number (0-9)
  • No spaces and special characters are allowed except underscore (_) and dollar sign ($).
  • The variable name can begin with an underscore (_) or a dollar sign ($)

Var Syntax

In dart, we can declare any kind variable using var keyword followed by the variable name and values assigned to it just like javascript. In this case, dart act as loosely type programming language.

void main() {
  var num = 12; /* number variable */
  var str = "Hello, World!"; /* string variable */
  var boolean = true; /* boolean variable */
  var list = [1, 3, 5, 7]; /* list variable */

  print(num);
  print(str);
  print(boolean);
  print(list);
}
Sample Input Sample Output
No Input 12
Hello, World!
true
[1, 3, 5, 7]

That means we can declare all kinds of variable using the var keyword.

Type Syntax

In the var syntax, we just saw, we can declare any kind of variable using the var keyword. But In dart, we can do it in another way. We can declaring variables by declaring the data type instead of var keyword. And it works the same.

void main() {
  int num = 12; /* number variable */
  String str = "Hello, World!"; /* string variable */
  bool boolean = true; /* boolean variable */
  List list = [1, 3, 5, 7]; /* list variable */

  print(num);
  print(str);
  print(boolean);
  print(list);
}
Sample Input Sample Output
No Input 12
Hello, World!
true
[1, 3, 5, 7]

Here int, String, bool, List these are data type in Dart. We will learn all the data types in the immediate next chapter.

The same results. So why, type syntax instead of var syntax?

This allows to check type checking while declaring a variable. And this is extremely useful feature for data type checking while initialize a value to a specific type of variable.

If an initialized value is not match with the datatype. Dart will throw an error.

void main() {
  int num = "Hello, World!";
  /* expecting number value 
     but assigned string text
  */
}

Final and Const

If you want to declare a variable for only once you can use final (for instance variable) and const (for remaining) keyword while declaring a variable. Once a variable is declared and initialized with final or const keyword, the value of the variable can’t be changed in once cycle of execution time.

void main() {
  // rules of declaring final and const variables
  final name = 'Bob'; // Without a type annotation
  const String nickname = 'Bobby'; // with the type annotation

  // re-assigned or changing value of final or const variable
  name = "Jhon Doe";  // throughs an error
}
Sample Input Sample Output
No Input Error: Can’t assign to the final variable ‘name’.

Late Variable

Another cool feature in Dart is the late variable declaration. Before understanding late variable we have to understand the null value in Dart.

Null Value

In Dart, we can keep a variable uninitialized in an entire program. Using an uninitialized variable will return the Null value, which is also the type of Null – another data type in Dart.

Okay, go back to the late variable. If we define a late variable, still we can keep it uninitialized same as the normal variable. But while runtime, If we fail to initialize the variable we will get an error. That means it allows us the blank field but not nullable.

Posted in