Working with Lists, Sets, and Maps
Real programs handle many values at once: the items in a cart, the users logged in, the settings for an app. Dart gives you three main containers for this, called collections. A List keeps values in order. A Set keeps each value only once. A Map links keys to values, like a dictionary links words to definitions.
Lists: Ordered Values
A List is a numbered sequence. The first item is at position 0, the second at position 1, and so on.
void main() {
final fruits = ['apple', 'banana', 'cherry'];
print(fruits[0]); // apple
print(fruits.length); // 3
fruits.add('date'); // add at the end
fruits.remove('banana');
print(fruits); // [apple, cherry, date]
}You can also reach items from the end: fruits.last is the final item, and fruits.first is the first.
Sets: Each Value Once
A Set stores values with no duplicates and no fixed order. Adding a value that is already there does nothing.
void main() {
final tags = {'dart', 'tutorial'};
tags.add('dart'); // ignored, already present
tags.add('beginner');
print(tags.contains('dart')); // true
print(tags.length); // 2
}A common trick: convert a List to a Set to remove duplicates.
final messy = ['a', 'b', 'a', 'c'];
final unique = messy.toSet().toList(); // [a, b, c]Maps: Keys and Values
A Map pairs each key with one value. You look things up by key, not by position.
void main() {
final ages = {'Ada': 36, 'Alan': 41};
print(ages['Ada']); // 36
ages['Grace'] = 45; // add a new pair
ages['Alan'] = 42; // update an existing value
for (final entry in ages.entries) {
print('${entry.key} is ${entry.value}');
}
}If a key is missing, ages['Bob'] returns null rather than crashing. This connects directly to the null safety ideas from earlier chapters.
Picking the Right Collection
Choosing well makes your code simpler and faster.
| Need | Use | Why |
|---|---|---|
| Ordered items, duplicates allowed | List | Index access, keeps order |
| Unique values | Set | Fast membership checks |
| Lookup by name or ID | Map | Instant find-by-key |
callout tip If you find yourself checking "is this value already in my List?" in a loop, you probably wanted a Set. The contains check on a Set is fast even for large collections.
What Comes Next
Collections become far more powerful when you transform them: turn every item into something else, keep only some items, or boil a list down to a single value. That is the subject of the next chapter, where we meet map, where, and reduce.
Exercise
You are storing the email addresses of people who signed up, and you must never store the same email twice. Which collection should you use, and why?
Show answer
A Set. It automatically refuses duplicates, so adding the same email twice stores it only once, and checking whether an email is already present is fast.
Exercise
What does ages['Bob'] return if the map ages has no 'Bob' key?
Show answer
It returns null. Dart does not crash on a missing Map key; it gives you null, which you can handle with null-safe operators like ??.
Spotted a mistake, or code that does not work?