Dart Programming — Learning Variables.
By: Yogita Kumar
Tags: App Development, Flutter, Dart, Programming, Flutter App Development

Variables are used to store information to be referenced and manipulated in the program. Variables are nothing but a kind of container that holds a piece of information. The main purpose of variables is to store and label data in memory, that can be used later on.

Dart language supported the following types:

Numbers

Strings

Boolean

Lists

Maps

Numbers

Numbers in dart represent numeric literals. Programmer can define Numbers as Integer or as Double

Integer: Integer values represent non-fractional values (numeric values without decimal point) e.g. “5”

declared as:

int a = 5;

int is a keyword to declared it as an Integer type, a is the variable name, = is assignment operator, 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. “5.5”

Declared as,

double b = 5.5;

double is a keyword to declare it as Double type, b is a variable name, = is assignment operator, 5.5 is the value of variable b , ; indicates the end of the statement.

2. String

Strings represents a sequence of characters. For Example, if a programmer wants to store first name, last name, etc. the String data type should be used.

declared as:

String first_name = “XYZ”;

String is the keyword to declared it as String type, first_name is a variable name, = is assignment operator, XYZ is value for a string variable.

String values are enclosed in either single or double quotes.

3. Boolean

The Boolean data type represents Boolean values true and false.

declared as:

bool isEnabled = false;

bool is the keyword to declared it as Boolean type, isEnabled is a variable name, = is assignment operator, false is value for the Boolean variable.

4. List

A List is an ordered group of objects. List are similar to an array.

5. Map

Map data type represents a set of key-value pairs.

There are some rules while defining variable names:

All variable names must begin with a letter of the alphabet or an underscore( _ ).

After the first initial letter, variable names can also contain letters and numbers.

Variable names are case sensitive.

No spaces or special characters are allowed, except underscore(_) and the dollar($) sign

You can’t use a keyword (a reserved word) as a variable name.

var keyword:

A variable must be declared before it is used. In Dart, the var keyword does the same job.

var city_name=’London’;

dynamic keyword:

Variables that declared without a static type are implicitly declared as dynamic.

dynamic name=”XYZ”;

Difference between var and dynamic :

dynamic: can change the TYPE of the variable, and can change the VALUE of the variable later in code.

var: can’t change TYPE of the variable, but can change VALUE of the variable later in code.

Dart supports type-checking by prefixing variable names with a data type. Type checking is all about ensuring that the program is type-safe, meaning that the possibility of type errors is kept to a minimum.

final and const keyword:

final and const keywords are used to declared constants. Changing values of a variable declared using final and const is not allowed.

Dart throws an exception when a programmer attempts to modify variables declared as final or const

Syntax,

final var_name;

OR

final data_type var_name;

&

const var_name;

OR

const data_type var_name;

Thank You!