Variables are mainly used to store information. The main objective of the variable is to store data in memory that can be used later on. The values can be stored in the form of integer, boolean, string, lists and maps.
Dart uses a var keyword to declare the variable.
The syntax of var is defined below
|
The 'var' keyword is used to declare variables in dart.
For instance,
1
var name="smith"; |
In the above example, the variable name has allocated some space in the memory. The semicolon(;) is necessary to use because it separates a program statement from another.
Dart language supported the following types:
1- Number
Numbers are the numeric value used in dart language. Programmers can define Numbers as Integer or as Double.
Integer: Integer values represent non-fractional values (numeric values without decimal point) e.g. “9”
declared as:
int a = 5; |
int is a keyword to declare it as an Integer type, a is the variable name, 5 is the value of variable a, ; indicates the end of the statement.
Double: Double values represents fractional values (numeric values with a decimal point) e.g. “2.2”
Declared as,
double b = 5.5; |
double is a keyword to declare it as Double type, b is a variable name, 5.5 is the value of variable b, ; indicates the end of the statement.
Example:
void main() { int num=4; double numa=5;
print(num); print(numa); } |
Output:
4.
5.
2- String
Strings represent a sequence of characters. For Example, if a programmer wants to store first name, address, etc. the String data type should be used.
declared as:
String first_name = “Rilee”; |
String is the keyword to declare it as String type, first_name is a variable name, = is assignment operator, Rilee is value for a string variable.
String values are enclosed in either single or double quotes.
You can add variables inside of a string. Use a $ dollar sign before the variable name:
var name = 'Ali';
const age = 15;
var page = '$name is $age.';
Example:
void main() { String address="Mumbai, India"; print(address); } |
Output:
Mumbai,India
3 - Boolean
The bool type is for Boolean values, that is , “true” or “false”.
The boolean value of the declaration is ‘true’ if the declared value
Is the string “true”,and false if the value is “false”.
Booleans are commonly used in decision making statements.
Example:
bool isLoading = true;
Or
bool isLoading = false;
4 - List
A list is an ordered group of objects mostly used for large amounts of data. Lists are similar to an array.
You use < > angle brackets to say what type of list it is, so <String> means it’s a list of strings. To make a list you use [ ] square brackets. Add commas to separate the things inside the list.
Example:
var country = [ Iran, Afghanistan, USA, UK, China] ; const numbers = [ 20, 39, 49, 20] ; |
5 - Map
Map contains keys and values. Every key has its own value.Value is stored in the key.
Example:
Here is an example:
Map<String, int> ages = { “Bilo” : 34 , “Ali” : 33 , “Jordan”: 12};
main(List<String> args) { var list = [1,2,3,4];
print(list); //Output: [1, 2, 3, 4] //Length print(list.length);
//Selecting single value print(list[1]); //Output: 2
//Adding a value list.add(10);
//Removing a single instance of value list.remove(3);
//Remove at a particular position list.removeAt(0); } |
If we want to define a compile-time constant list, for example, the contents of the list are immutable, you can use keywords const
main(List<String> args) { var list = const [1,2,3,4]; } |