Dart supports various data types, allowing you to store and manipulate different kinds of data. Here’s a concise overview of the basic data types in Dart with examples:
1. Numbers
Dart has two types of numbers: `int` for integer values and `double` for floating-point values.
void main() {
int age = 25;
double height = 5.9;
print('Age: $age, Height: $height');
}
- Strings
Strings represent a sequence of characters.
void main() {
String greeting = 'Hello, Dart!';
String name = 'Alice';
print('$greeting My name is $name.');
}
- Booleans
Booleans represent true or false values.
void main() {
bool isDartFun = true;
bool isSnowing = false;
print('Is Dart fun? $isDartFun');
print('Is it snowing? $isSnowing');
}
- Lists
Lists are ordered collections of items.
void main() {
List<int> numbers = [1, 2, 3, 4, 5];
List<String> fruits = ['Apple', 'Banana', 'Cherry'];
print('Numbers: $numbers');
print('Fruits: $fruits');
}
- Sets
Sets are collections of unique items.
void main() {
Set<int> uniqueNumbers = {1, 2, 3, 3, 4};
Set<String> uniqueFruits = {'Apple', 'Banana', 'Cherry', 'Apple'};
print('Unique Numbers: $uniqueNumbers'); // {1, 2, 3, 4}
print('Unique Fruits: $uniqueFruits'); // {Apple, Banana, Cherry}
}
Difference between List and Sets
List
- Ordered: Elements in a List are ordered. This means each element has an index and can be accessed by its position.
- Duplicates: A List can contain duplicate elements.
Set
- Unordered: Elements in a Set are unordered. There is no index, and elements cannot be accessed by position.
Unique Elements: A Set cannot contain duplicate elements. Each element is unique.
void main() {
// Using List to keep track of enrollment order
List<String> studentEnrollmentOrder = [];
studentEnrollmentOrder.add("Alice");
studentEnrollmentOrder.add("Bob");
studentEnrollmentOrder.add("Alice"); // Duplicate allowed
studentEnrollmentOrder.add("Charlie");
// Print student enrollment order
print("Student Enrollment Order:");
for (var student in studentEnrollmentOrder) {
print(student);
}
// Using Set to keep track of unique event registrants
Set<String> eventRegistrants = {};
eventRegistrants.add("Alice");
eventRegistrants.add("Bob");
eventRegistrants.add("Alice"); // Duplicate not allowed, will be ignored
eventRegistrants.add("Charlie");
// Print unique event registrants
print("\nUnique Event Registrants:");
for (var registrant in eventRegistrants) {
print(registrant);
}
}
- Maps
Maps are collections of key-value pairs.
void main() {
Map<String, int> scores = {
'Alice': 90,
'Bob': 85,
'Charlie': 95
};
print('Scores: $scores');
print('Alice\'s score: ${scores['Alice']}');
}
```
- Runes
Runes represent Unicode characters.
void main() {
String heart = '\u2665'; // Unicode for heart symbol
print('Heart: $heart');
}
- Symbols
Reflective programming, also known as reflection, is a programming technique where a program can inspect and modify its own structure and behavior at runtime
Symbols are used to refer to identifiers by name, primarily in reflection.
void main() {
Symbol libraryName = #dart.core;
print('Library Symbol: $libraryName');
}
- Null Safety
By default, variables cannot be null unless specified with `?`.
void main() {
int? nullableInt = null;
String? nullableString = null;
print('Nullable int: $nullableInt');
print('Nullable string: $nullableString');
}
These data types cover most of the basic needs for storing and manipulating data in Dart programs.
