Deep Dive into Dart Variables


Home » Dart » Deep Dive into Dart Variables

Dart is a programming language that was introduced by Google in 2011. It is an object-oriented, class-defined, garbage-collected language that supports both static and dynamic typing. In Dart, variables are used to store data, and they play a crucial role in the programming process. In this blog post, we will explore Dart variables in detail, including their types, declaration, initialization, scope, and more. Learning Dart language is fun, so let’s get started!

Deep Dive into Dart Variables

What are variables in Dart?

A variable is a named storage location in a program where you can store a value. In Dart, variables can hold values of various types, including numbers, strings, Booleans, lists, and objects. The type of a variable is determined by the data it holds.

Dart has several types of variables, including:

Numbers: Dart supports various types of numbers, such as int, double, and num. The int type is used to represent integers, while the double type is used to represent floating-point numbers. The num type is used to represent either int or double, depending on the value assigned.

Strings: In Dart, strings are used to represent textual data. A string is a sequence of characters that can be enclosed in single or double quotes.

Booleans: Booleans are used to represent true or false values. In Dart, the boolean type is denoted by the keyword bool.

Lists: A list is an ordered collection of objects in Dart. Lists are created using square brackets and can contain objects of any type.

Maps: Maps are used to associate keys with values in Dart. They are created using curly braces and consist of a set of key-value pairs.

Declaration of Variables in Dart

In Dart, variables are declared using the var keyword followed by the variable name and an optional type annotation. For example, the following code declares a variable named age and assigns it the value 30:

var age = 30;

If you want to explicitly declare the type of the variable, you can use the type annotation. For example, the following code declares a variable named name of type String:

String name = 'John';

There are other types of variables in Dart, including final and const variables.

Final variables

A final variable is a variable whose value can be set once and cannot be changed. Once you assign a value to a final variable, you cannot change it. To declare a final variable, use the final keyword. For example:

final int age = 30;

In the above example, we declare a final variable named age and initialize it with the integer value 30. Once assigned, the value of the age variable cannot be changed.

Const variables

A const variable is a variable whose value is known at compile time and cannot be changed at runtime. To declare a const variable, use the const keyword. For example:

const double pi = 3.14159;

In the above example, we declare a const variable named pi and initialize it with the value of the mathematical constant π. Once assigned, the value of the pi variable cannot be changed at runtime.

Working with variables in Dart

Once you have declared a variable in Dart, you can perform various operations on it, such as assigning new values, manipulating the value, or comparing it to other values.

Assigning values to variables

To assign a new value to a variable in Dart, simply use the assignment operator = followed by the new value. For example:

var myAge = 30;
myAge = 31;

In the above example, we first declare a variable myAge and initialize it with the integer value 30. We then assign a new value of 31 to the same variable. To practice the below example you can use the Dart Pad for executing the programs online.

Arithmetic operations

void main() {

 var x = 10;
 var y = 20;
 var sum = x + y; // 30
 print(sum); 
}

In the above example, we declare two variables x and y and initialize them with integer values. We then declare a third variable sum and assign it the sum of x and y.

String concatenation

tring concatenation is the process of combining two or more strings into a single string. In Dart, string concatenation can be performed using the + operator or the adjacent string literals.

To concatenate two or more strings using the + operator, simply use the + symbol between the strings. For example:

void main() {

var firstName = 'Nikin';
var lastName = 'SK';
var fullName = firstName + ' ' + lastName;
print(fullName); // Nikin SK
}

In the above example, we declare two string variables firstName and lastName and initialize them with the values ‘Nikin’ and ‘SK’ respectively. We then concatenate the two strings using the + operator and assign the result to a third variable fullName. Finally, we print the value of fullName to the console, which outputs “Nikin SK”.

You may also like...