Dart Variables and Data Types

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Dart variables and Data types

In Dart, variables play a crucial role in storing and manipulating data. To effectively work
with variables, it is essential to understand the different data types available in Dart. This
chapter will introduce you to the concept of variables in Dart and explain each data type in
detail. By the end of this chapter, you will have a solid understanding of Dart variables and
how to use them effectively in your Flutter applications.

I - Variables and Data Types


In programming, a variable is a named storage location that can hold a value. Dart is a
statically typed language, which means you need to declare the data type of a variable
before using it. Dart provides several built-in data types that you can use to declare variables.
These data types include:
1. : Represents integer values. It can hold both positive and negative whole numbers
int
without decimal points.
2. double : Represents floating-point numbers. It can store decimal values and provides a
higher precision than int .
3. bool : Represents a boolean value, which can be either true or false . Booleans are often
used in conditional statements to control program flow.
4. String : Represents a sequence of characters. It is used to store textual data, such as
names, messages, and other string literals.
5. List : Represents an ordered collection of objects. It is also known as an array in other
programming languages. A list can contain elements of any data type, including other
lists.
6. Set : Represents an array of unique values. When we try to add data to a Set with the
name.add() method, it ensures that the data is not duplicated.
7. Map : Represents a collection of key-value pairs. It allows you to associate values with
unique keys, similar to a dictionary or an associative array in other languages.
8. dynamic : Represents a variable with dynamic typing. A dynamic variable can hold values
of any data type and its type can change at runtime.
9. var : Represents a variable with type inference. The type of a var variable is inferred
from the assigned value. Once assigned, the type of the var variable cannot be changed.
10. final : Represents an immutable variable, which can only be assigned a value once. The
value of a final variable cannot be changed once it is assigned.
11. const : Represents a compile-time constant variable. The value of a const variable is
known at compile time and remains constant throughout the program's execution.
II - Declaring Variables
To declare a variable in Dart, you use the var , final , or const keyword followed by the
variable name and an optional type annotation. Here are a few examples of variable
declarations:

var age = 25; // Inferred type: int


final double pi = 3.14; // Explicit type annotation: double
const String appName = 'MyApp'; // Explicit type annotation: String
dynamic dynamicVariable = 'Hello'; // Dynamic typing
List<int> numbers = [1, 1, 3, 4, 5]; // List with type annotation
Set<String> names = {'Adam', 'Jamila', 'Yassine', 'Brahim'} // Set with type annotation
Map<String, int> scores = {'John': 90, 'Alice': 95}; // Map with type annotations

III - Working with variables


Once you have declared a variable, you can assign values to it and perform operations based
on its data type. Here are a few examples of working with variables:

int a = 10;
int b = 20;
int sum = a + b; // Addition

double x = 3.14;
double y = 2.0;
double product = x * y; // Multiplication

String name = 'John';


String greeting = 'Hello, $name!'; // String interpolation

bool isRaining = true;


bool isSunny = false;
bool isWeatherGood = isRaining || isSunny; // Logical OR

List<int> numbers = [1, 2, 3, 4, 5];


int firstNumber = numbers[0]; // Accessing list element
// Same for the Set

Map<String, int> scores = {'John': 90, 'Alice': 95};


int johnScore = scores['John']; // Accessing map value

IV - Type Safety and Type Inference


Dart is a statically typed language, which provides type safety at compile-time. Type safety
ensures that variables are used in a manner consistent with their declared types. If you
attempt to assign a value of the wrong type to a variable or perform incompatible operations,
the Dart analyzer will raise a type error during compilation.
Dart also supports type inference, where the type of a variable is determined automatically
based on the assigned value. The var keyword allows you to declare variables with inferred
types. Dart's type inference mechanism analyzes the assigned value and assigns an
appropriate type to the var variable. Once the type is inferred, it cannot be changed.
Type inference is helpful in reducing boilerplate code and making your code more concise,
while still maintaining type safety. However, it's important to strike a balance and use explicit
type annotations when clarity and readability are paramount.

You might also like