Lists (Arrays)
In Dart, a List is an ordered group of items.
dart
Sets (Unique)
A Set is an unordered collection of unique items. It is much faster than a List for checking if an item exists.
dart
Maps (Key-Value)
A Map is a collection of key-value pairs.
dart
Collection Operators
Wait! Dart has some very powerful operators for combining and manipulating collections.
- Spread Operator (
...): Insert all elements of one collection into another. - Collection If: Add an item to a list only if a condition is true.
- Collection For: Build a list based on another list.
dart
Summary
| Type | Best Use Case |
|---|---|
| List<T> | Ordered sequence of items (Default) |
| Set<T> | Collection of unique items |
| Map<K, V> | Key-value lookups |
| ... | Merging collections |
| forEach | Running a function for every item |
| map | Transforming every item (returns an Iterable) |
| Best For | Organizing and processing application data |
| Logic | Strong types ensure all items match the collection type |
| Safety | Generics prevent adding the wrong type of data |
| Modern | Collection If/For make UI code (Flutter) much cleaner |
| Standard | Collections are mutable by default; use const for immutability |
| Identity | Collections are objects that implement Iterable |