Variables in dart

Variables in dart

All 7 types of variables in dart to use in flutter ๐Ÿค

ยท

4 min read

1. Variables

In dart, there are many data types just like other languages,

1. int , double, numbers

To store digits

int number = 0; //this is integer as usual.

double number2 = 3.1415; // this is a double value which can store decimals also

// simpler, we can initialize it like

num value = 1;

num value2 = 3.44;

// to print out the data type

print(value2.runtimeType); // prints double

2. String

strings are data types which store characters.


String name = "Aadhithyan";

print(name); // Aadhithyan

print(name.toUpperCase()); // prints it in all caps ie "AADHITHYAN"
// similarly .toLowerCase()
// ctrl space to see more functions

3. bool

Boolean data type store true / false

bool isTrue = true;
bool isFalse = false;

// use cases:
// flags, conditions, etc..

4. var

instead of doing the above, theres a simpler way to define data types, and thats by using the keyword var while initializing. var is short for variable. and it can be used for any data type.

examples:

var isString = "this is a string";

var isNum = 17.09;

var isBool = true;

print(isNum.runtimeType); // double

5. final and const

i) final:

while declaring a variable as final we cannot change the value after declaration.

example:

final breathing = true;

ii) const:

similar to final but slightly different

const pi = 3.14;

6. lists

for storing multiple objects of the same data type.

var names = *new* List(4);

names[0] = "Aadhithyan";
names[1] = "Me2";
names[2] = "Me3";
names[3] = "Me4";

easier way of doing this

var names = [
    "Name1",
    "Name2",
    "Name3"
];

for each too loop through the list:

names.forEach((item){
    print(item);
});

7. map

Map is an object that stores data in the form of a key-value pair. Each value is associated with its key, and it is used to access its corresponding value. Both keys and values can be any type.

var maps = {
    "name": "Aadhithyan",
    "age": 15,
    "hobby": "Coding"
};

print(maps.key("name"));

keys are the left hand items: eg name, age, hobby


These are all the variables in dart! hope you learned something, and I'll see you in the next one! ๐Ÿ‘‹

PS: Thanks to Dhruv#2018 for the clean thumbnail