Dart Variables
Dart Variables
A variable is a name given to a memory location used to store data that can be accessed by calling its name. In this tutorial, we will learn how to declare Dart variables.
Depending on the type of data that needs to be stored, different variables are used to store it.
Variable Declaration Syntax
In Dart, you can declare a variable using the var keyword followed by the variable name and an optional type annotation.
var age = 27;
var websiteName = ‘TestingDocs’;
Although Dart can infer the type of a variable, you can also explicitly specify the type using a type annotation.
To declare a variable, the syntax is as follows:
datatype variableName;
For example:
int age = 27;
String websiteName = ‘TestingDocs’;
bool isValid = true;
Dart supports type-checking, meaning it checks whether the data type and the data a variable holds are consistent. We can also declare multiple variables using a single declaration statement. To declare multiple variables of the same data type, the syntax is as follows:
datatype variable1Name, variable2Name;
Naming Rules
The rules for declaring variable names are as follows:
- Variable names cannot be reserved words or keywords. We should avoid using the keywords as the variable’s name in the code.
- Variable names cannot start with a number.
- Variable names can contain both alphabets and numbers.
- Variable names cannot contain spaces or special characters. The allowed special characters are underscore(_) and dollar sign($).
More information on the Dart programming language at: