Variables & Their Types in Dart for 2025
A Beginers Guide On Dart Variables, Their Types, Naming Convention & Rules
Variables are containers in dart. They contain the reference to their value. The reference is of memory location where the value is stored. When a programmer accesses the variable, compiler returns the value that is available at that particular location in the memory.
There are different types of variables, and contain values according to their types. This is called Type Casting or Type Annotation in Dart. I have shown in below how to write the write a variable, and assign value to it.
var variableName = "value";
“var” keyword indicates that this is a variable but it doesn’t help us out with its type. We can know that with its value. In above case the type is “String”. Anything in between double quotations is String.
Variable Declaration
Creating a variable, and not assigning any value to it is called variable declaration.
var variableName;
I have declared the variable. I haven’t initialized it.
Variable Initialization
Creating a variable, and also assigning the value to it is called variable initialization.
var variableName = "value";
I have declared and initialized the variable.
“variableName” is the name of the variable, which is the reference to the location where the “value” is stored. “var” indicates that it is a variable. Note that adding “;” is necessary. It is called Termination Sign.
Type Annotation or Type Casting
There are different types of variables in dart according to the type of value they are responsible to keep in the memory.
The types of variables are listed below:
1. String
2. int
3. double
4. number
5. bool
String
Anything in between double quotation (“”) or single quotations (‘’) is called String.
String variableName = "dummy string";
int
It represents integer numbers. It includes all values which are equal to, less and greater than zero. It doesn’t include decimal numbers.
int zero = 0;
int negativeNumber = -12;
int positiveNumber = 23;
Following is Incorrect:
int wrongIntegerNumber = 12.3; // it is wrong because 12.3 is decimal.
double
It represents all decimal numbers. It doesn’t include integer numbers.
double decimalNumber = 12.3;
Following is Incorrect:
double wrongDecimalNumber = 23;
num
It is combination of both “int” and “double”. It can contain integer as well as decimal value.
num integerExample = 23;
num decimalExample = 34.43;
bool
It can contain only two types of values. It can either contain true or false.
bool isDartEasy = true;
bool isProgrammingNotEasy = false;
Example Code 1:
main(){
String userName = "Ali";
int userAge = 23;
double userHeight = 5.6;
bool isLicensed = true;
print("User Information");
print("Name: $userName");
print("Age: $userAge");
print("Height: $userHeight");
print("License Status: $isLicensed");
}
From above example code, you can understand that you can access the value of variable adding $ sign in its beginning. In line 2 for example, $userName shows the actual name of the user that I have mentioned above.
print() is a method that allows us to print anything, and accessing the variable inside the print statement along with its description or adding more information about it is called String Interpolation.
print("user name is ${userName}");
Output:
user name is Ali
Code Example: 2
main(){
String userName = "Ali";
int userAge = 23;
double userHeight = 5.6;
bool isLicensed = true;
print("User Information");
print("Name: ${userName}");
print("Age: ${userAge}");
print("Height: ${userHeight}");
print("License Status: ${isLicensed}");
}
Code Example 1 & 2 have same output.
It is important to know that are some rules and regulations which are essential to follow while creating variables in dart. They are listed below.
1. Variable name can comprise letters and numbers.
2. Variable name can’t start with number, it can only begin with letter.
3. Variable name can’t have space in between e.g.: “variable Name” is incorrect
4. It can’t contain special correctors like #, %, @, etc. except $, and underscore (_).
5. Variable name is case sensitive meaning A is not same as a.
6. It can’t be name of a keyword.
Naming Convention:
In programming, and in dart specifically too, there is a particular pattern of naming the variables. The name of variable should start with small letter, and if its name comprise more than one word then first letter of each world should be capital. This naming convention is called lowerCamelCase. It is widely used in programming to name variables, functions, and methods.
Variable name with one word.
var name = "Ali";
Variable name with more than one word.
var userName = "Ali";
var userSecondName = "Ahmed";
It is essential to name the variables, functions, and classes according to the naming convention, and name them meaningfully because it increases the code readability however, if you don’t follow them, the code executes anyway without any error. Compiler will show the warning though.
Conclusion:
In this article, I have explained about Dart Variables, and their types with code example. In addition to it, I have also mentioned about naming convention, and rules to create and name them in simple and understandable language. I hope you like this article. Thank you for reading this. Regards!