Dart Variables

In this tutorial you will learn about the Dart Variables and its application with practical example.

Dart Variables

Variables is an identifier used to refer memory location in computer memory that holds a value for that variable, this value can be changed during the execution of the program. When you create a variable in Dart, this means you are allocating some space in the memory for that variable. The size of memory block allocated and type of the value it holds is completely dependent upon the type of variable.

Rules for naming a variable in Dart

Constant and variable names cannot contain whitespace characters, mathematical symbols, arrows, private-use (or invalid) Unicode code points, or line- and box-drawing characters. Naming a variable in Dart Programming is an important task and must follow some rules, listed below –

  • Variable name can consist of letter and alphabets.
  • Keywords are not allowed to use as a variable name.
  • Blank spaces are not allowed in variable name.
  • First character of variable should always be alphabet and cannot be digit.
  • Variable name are case sensitive i.e. UPPER and lower case are significant.
  • Special characters like #, $ are not allowed except the underscore (_) and the dollar ($) sign.

Recommendation :- Variable name must be readable and should be relative to its purpose.

Declaring Variables In Dart

In Dart, a variables must be declared before they are used. Variables are declared using the var keyword followed by variable name that you want to declare. Dart is a type inferred language, which allows compiler automatically infer(know) the type of data we want to store based on the initial value we assign.

Syntax:-

or

Example:-

This statement means we’re declaring some space for a variable called counter. Note that the semicolon at the end of the line; that is how your compiler separates one program statement from another.

Type Annotations

Although Dart is a type inferred language, you can optionally provide a type annotation while declaring a variable to suggest type of the value variable can hold. In Dart, by prefixing the variable name with the data type ensures that a variable holds only data specific to a data type.

Syntax:-

or

Example:-

Here is an example of declaring an integer, which we’ve called counter. This statement means we’re declaring some space for a variable called counter, which will be used to store integer data.

Declaring multiple variable

In Dart, it is possible to declare multiple variables of same type in a single statement separated by commas, with a single type annotation as following-

Syntax:-

Example:

Variable assignment In Dart

The assignment operator (=) is used to assign values to a variable, the operand in the left side of the assignment operator (=) indicates the name of the variable and the operand in the right side of the assignment operator (=) indicates the value to be stored in that variable.

Example:-

Initializing Variable In Dart

In Dart, it is possible to declare and assign some initial value to a variable in single statement.

Syntax:-

Example:-

Default Value

In Dart, uninitialized variables are provided with an initial value of null. Even variables with numeric types are initially assigned with null value, because numbers like everything else in Dart are objects.

Example:-

In this tutorial we have learn about the Dart Variables and its application with practical example. I hope you will like this tutorial.