In Dart, data types define the type of data that can be stored and manipulated within variables. Since Dart is a strongly typed language, every variable is associated with a specific data type. Understanding the various data types in Dart is essential for writing effective and efficient code. This post covers the basic data types in Dart, including their usage and examples.
For further information on detailed topics of Dart, click here.
1. Numbers
Dart provides two types of numeric data types: int
and double
.
-
int: Represents integer values without a decimal point. It can hold both positive and negative whole numbers.dartCopy code
int age = 25; int year = 2024;
- double: Represents floating-point numbers with a decimal point. It is used for representing numbers with fractions.
int age = 25;
int year = 2024;
Operations on Numbers:
You can perform various arithmetic operations on numbers, such as addition, subtraction, multiplication, and division.
int sum = 10 + 5;
double division = 10 / 3;
2. Strings
Strings in Dart represent a sequence of characters enclosed within single or double quotes. You can use them to store and manipulate textual data.
String name = 'Alice';
String greeting = "Hello, World!";
String Interpolation:
You can use string interpolation to embed expressions within strings.
String fullName = '$name Smith'; // Output: Alice Smith
Multiline Strings:
You can create multiline strings using triple quotes.
String multiline = '''
This is a
multiline string
in Dart.
''';
3. Booleans
Booleans represent true or false values. They are used for logical operations and decision-making in code.
bool isActive = true;
bool isCompleted = false;
Boolean Operations:
You can perform logical operations such as AND, OR, and NOT on boolean values.
bool result = isActive && isCompleted; // false
bool opposite = !isActive; // false
4. Lists
Lists in Dart are ordered collections of items. They are similar to arrays in other programming languages and can hold multiple values of the same or different data types.
List<int> numbers = [1, 2, 3, 4, 5];
List<String> names = ['Alice', 'Bob', 'Charlie'];
Accessing Elements:
You can access elements in a list using an index, starting from 0.
int firstNumber = numbers[0]; // Output: 1
String firstName = names[0]; // Output: Alice
Modifying Lists:
You can add or remove elements from a list.
numbers.add(6); // Adds 6 to the end of the list
names.remove('Charlie'); // Removes 'Charlie' from the list
5. Maps
Maps in Dart represent key-value pairs. They are similar to dictionaries or hashmaps in other languages and are used to store and retrieve values based on unique keys.
Map<String, int> ages = {
'Alice': 25,
'Bob': 30,
};
Accessing Elements:
You can access values in a map using keys.
int aliceAge = ages['Alice']; // Output: 25
Modifying Maps:
You can add or remove key-value pairs in a map.
ages['Charlie'] = 28; // Adds 'Charlie' with age 28
ages.remove('Bob'); // Removes 'Bob' from the map
6. Sets
Sets in Dart represent unordered collections of unique items. They are useful when you need to store items without duplicates.
Set<int> uniqueNumbers = {1, 2, 3, 4, 5};
Adding Elements:
You can add elements to a set, and duplicates will be ignored.
uniqueNumbers.add(6); // Adds 6 to the set
uniqueNumbers.add(3); // Ignored as 3 is already in the set
7. Null
The Null
type in Dart represents the absence of a value. By default, Dart variables cannot hold null
values unless explicitly allowed.
int? nullableNumber = null;
Null Safety:
Dart supports null safety, which helps you avoid null-related errors by requiring you to handle null explicitly.
int? number;
if (number != null) {
print(number);
}
8. Dynamic
The dynamic
type allows a variable to hold values of any type. However, using dynamic
bypasses type checking, which can lead to runtime errors.
dynamic value = 10;
value = "Hello"; // Allowed, but risky
Conclusion
Understanding Dart’s data types is essential for writing robust and efficient code. Whether you’re dealing with numbers, strings, or more complex structures like lists and maps, knowing how to use and manipulate these types effectively will greatly enhance your programming experience. By mastering these fundamental data types, you can build more reliable and maintainable Dart applications.
Happy Coding…!!!
Leave a Reply