Variables and Data types

Variables and Data types

What are variables in Dart ?

In Dart, variables are used to store data values. Each variable has a specific data type, which determines the kind of values it can hold.

Dart is a statically typed language, which means that variables must be declared with their data types before they can be used.

You are familiar with the topic ? Check your knowlege

Variable Declaration

Rules for declaring variable in dart

There are certain guidelines we need to follow while declaring the varibales. They are:

  • A variable name must start with a letter or the underscore character _.
  • A variable name cannot start with a digit.
  • A variable name can only contain alphanumeric characters and underscores (A-z, 0-9, and _ ).
  • Variable names are case-sensitive (age, Age and AGE are three different variables).

In Dart, variables can be declared using the var keyword followed by the variable name and an optional initial value. Here’s an example:

Example

var name = 'John';

In the above example, the variable name is declared using the var keyword. The variable name is name and the initial value is 'John'. The variable name is of type String because the initial value is a string.

The var keyword is optional. You can also declare variables using the data type of the initial value. Here’s an example:

String name = 'John';

In the above example, the variable name is declared using the String data type. The variable name is name and the initial value is 'John'. The variable name is of type String because the initial value is a string.

var keyword will understand the datatype itself after the value is assigned. This means variables declared using values assigned will convert to var to the datatype of value.

Variable Assignment

In Dart, variables can be assigned new values using the assignment operator =. Here’s an example:

Example

var name = 'John';
name = 'Jane';

Output

Jane

code Try Yourself

In the above example, the variable name is assigned the value 'John' and then the value 'Jane'.

Types of varibales

In dart, there are two types of varibales :

Type Scope
public When you define a varibale by default is public
private if you define a variable with the underscrore prefix (_) then its a private variables. Scopes of private variables are limited to a file or class

Public Variables

 int name = "Flutter World"; 

Private variables

 int _name = "Flutter World"; 

Datatypes in Dart

In Dart, variables can be declared using the following data types:

  • int - for integer values
  • double - for floating-point values
  • num - for numeric values
  • String - for string values
  • bool - for boolean values
  • List - for list values
  • Map - for map values
  • Set - for sets
  • dynamic - for dynamic values

We will discuss more about List, Map and Set in the Dart collections chapter.

int datatype in dart

In Dart, the int data type is used to represent integer values. Here’s an example:

Example

int age = 20;

In the above example, the variable age is declared using the int data type.

The variable name is age and the initial value is 20. The variable age is of type int because the initial value is an integer.

double datatype in dart

In Dart, the double data type is used to represent floating-point values. Here’s an example:

Example

double pi = 3.14;

In the above example, the variable pi is declared using the double data type.

The variable name is pi and the initial value is 3.14. The variable pi is of type double because the initial value is a floating-point(decimal) value.

num datatype in dart

In Dart, the num data type is used to represent numeric values. Here’s an example:

Example

num pi = 3.14;

In the above example, the variable pi is declared using the num data type. The variable name is pi and the initial value is 3.14. The variable pi is of type num because the initial value is a numeric value.

num can store both int and double values.

String Datatype in Dart

In Dart, the String data type is used to represent string values. Here’s an example:

Example

String name = 'John';

In the above example, the variable name is declared using the String data type. The variable name is name and the initial value is 'John'. The variable name is of type String because the initial value is a string value.

String Manipulation

Manipulation of string means to change the string in some way. Dart supports the following string manipulation methods:

  • Concatenation
  • Interpolation

String Concatenation in Dart

Concatenation is the process of joining two or more strings together. Dart supports the + operator for concatenation.

Syntax


string1 + string2

Example


void main() {
   String str1 = "Hello";
   String str2 = "World";
   print(str1 + str2);
}

Output

HelloWorld

code Try Yourself

String Interpolation in Dart

Interpolation is the process of inserting the value of an expression or variable into a string. Dart supports the $ operator for interpolation.

Syntax


string1 $expression string2

Example


void main() {
   String str1 = "Hello";
   String str2 = "World";
   print("$str1 $str2");
}

Output

Hello World

code Try Yourself

bool datatype in dart

In Dart, the bool data type is used to represent boolean values. Here’s an example:

Example

bool isMarried = false;

In the above example, the variable isMarried is declared using the bool data type. The variable name is isMarried and the initial value is false. The variable isMarried is of type bool because the initial value is a boolean value.

Boolean variables can hold true or false only

dynamic datatype in dart

In Dart, the dynamic data type is used to represent dynamic values. Here’s an example:

Example

dynamic age = 20;

In the above example, the variable age is declared using the dynamic data type. The variable name is age and the initial value is 20.

The variable age is of type dynamic because the initial value is a dynamic value.

Type Inference

In Dart, the var keyword can be used to declare variables without specifying their data types. Here’s an example:

Example

var name = 'John';

In the above example, the variable name is declared using the var keyword. The variable name is name and the initial value is 'John'. The variable name is of type String because the initial value is a string.

In Dart, the var keyword can also be used to declare variables without specifying their data types. Here’s an example:

var age = 20;

In the above example, the variable age is declared using the var keyword. The variable name is age and the initial value is 20. The variable age is of type int because the initial value is an integer.

Quiz

Check your knowlege with the quiz below.