Dart Lists Maps and Collections
Most apps handle groups of data — a list of products, a set of user tags, a dictionary of settings. Dart provides three main collection types: List, Map, and Set. Each serves a different purpose.
List — Ordered Collection
A List stores items in a specific order. Each item has an index starting at 0.
Index: [0] [1] [2] [3]
┌─────┬─────────┬─────────┬──────────┐
Items: │Apple│ Mango │ Banana │ Grapes │
└─────┴─────────┴─────────┴──────────┘
List<String> fruits = ['Apple', 'Mango', 'Banana', 'Grapes'];
print(fruits[0]); // Apple
print(fruits.length); // 4
fruits.add('Orange'); // Add at end
fruits.insert(1, 'Kiwi'); // Add at index 1
fruits.remove('Mango'); // Remove by value
fruits.removeAt(0); // Remove by index
print(fruits); // [Kiwi, Banana, Grapes, Orange]
Common List Methods
| Method | What It Does |
|---|---|
add(item) | Appends an item to the end |
insert(i, item) | Inserts at position i |
remove(item) | Removes first matching item |
contains(item) | Returns true if item exists |
sort() | Sorts list in place |
reversed | Returns items in reverse order |
where(test) | Filters items matching a condition |
map(fn) | Transforms each item |
Filtering and Transforming a List
List<int> scores = [45, 72, 88, 34, 91, 60]; // Keep only scores above 60 List<int> passed = scores.where((s) => s > 60).toList(); print(passed); // [72, 88, 91] // Double every score List<int> doubled = scores.map((s) => s * 2).toList(); print(doubled); // [90, 144, 176, 68, 182, 120]
Map — Key-Value Pairs
A Map stores data as key-value pairs, like a real-world dictionary where each word (key) has a definition (value).
Key Value ───────── ─────────── 'name' → 'Arjun' 'age' → 28 'city' → 'Delhi'
Map<String, dynamic> user = {
'name': 'Arjun',
'age': 28,
'city': 'Delhi',
};
print(user['name']); // Arjun
print(user['age']); // 28
user['email'] = 'arjun@mail.com'; // Add new key
user['age'] = 29; // Update value
user.remove('city'); // Delete a key
print(user.keys); // (name, age, email)
print(user.values); // (Arjun, 29, arjun@mail.com)
Iterating Over a Map
user.forEach((key, value) {
print('$key: $value');
});
// name: Arjun
// age: 29
// email: arjun@mail.com
Set — Unique Items Only
A Set is like a List but it never holds duplicates. Order is not guaranteed.
List (allows duplicates): [1, 2, 2, 3, 3, 3]
Set (unique only): {1, 2, 3}
Set<String> tags = {'flutter', 'dart', 'mobile'};
tags.add('flutter'); // Duplicate — ignored silently
print(tags); // {flutter, dart, mobile}
tags.add('web');
print(tags.contains('dart')); // true
Choosing the Right Collection
Need ordered, indexed items? → List Need key → value lookup? → Map Need unique items only? → Set
Spread Operator and Collection If
Dart supports powerful ways to build collections inline.
Spread Operator
List<int> a = [1, 2, 3]; List<int> b = [0, ...a, 4]; // Spreads a into b print(b); // [0, 1, 2, 3, 4]
Collection If
bool isAdmin = true; List<String> menu = [ 'Home', 'Profile', if (isAdmin) 'Admin Panel', // Included only if true ]; print(menu); // [Home, Profile, Admin Panel]
Lists Inside Flutter
Lists power most Flutter UIs. The ListView.builder widget takes a list and builds one row per item efficiently.
List<String> cities = ['Mumbai', 'Delhi', 'Bangalore'];
ListView.builder(
itemCount: cities.length,
itemBuilder: (context, index) {
return ListTile(title: Text(cities[index]));
},
)
┌─────────────────────────┐ │ Mumbai │ ├─────────────────────────┤ │ Delhi │ ├─────────────────────────┤ │ Bangalore │ └─────────────────────────┘
