What is a Set in Dart?
A Set is an unordered collection of unique items. Unlike a List, a Set cannot contain duplicate elements. This makes Sets ideal for tasks where you need to ensure uniqueness, such as storing a collection of tags, IDs, or any items where duplicates are not allowed. Dart's Set is implemented as a hash table, so most operations (add, remove, contains) are efficient.
Creating Sets
Dart provides several ways to create sets, depending on your needs.
- Set Literal
- Empty Set
⚠️ Important: Using {} alone creates a Map, not a Set. Always specify a type parameter or use a type annotation to create an empty Set.
- Using
Set.from()
Set.from()
- Using
Set.of()
Set.of()
- Using
Set.identity()
Set.identity()Creates an identity‑based set, which uses identical() rather than == for equality checks. Useful when you need to distinguish objects even if their content is the same.
- Unmodifiable Set
Adding Elements
Removing Elements
Checking for Elements
Set Operations (Mathematical)
Sets support mathematical set operations: union, intersection, and difference.
Union
Intersection
Difference
Iterating Over a Set
Converting Set to List
Set Properties
length– number of elements.
isEmpty– true if the set has no elements.
isNotEmpty– true if the set has at least one element.
first– first element in iteration order (throws if empty).
last– last element in iteration order (throws if empty).
hashCode– the hash code of the set.
Useful Set Methods
add(element)– adds an element if not already present.
addAll(iterable)– adds all elements from an iterable.
remove(element)– removes a single element.
removeAll(iterable)– removes all elements in the iterable.
retainAll(iterable)– keeps only elements present in the iterable.
clear()– removes all elements.
contains(element)– checks if an element exists.
containsAll(iterable)– checks if all elements exist.
difference(other)– returns a new set with elements not inother.
intersection(other)– returns a new set with common elements.
union(other)– returns a new set with elements from both sets.
lookup(element)– returns the element if found, else null (useful with custom objects).
elementAt(index)– returns the element at a given position in iteration order (slow for large sets).
Collection If and For
Just like lists, sets support collection if and for for conditional and computed elements.
Complete Example
When to Use a Set
- When you need to store unique elements (no duplicates).
- For fast lookups (
containsis O(1)).
- For fast lookups (
- For mathematical set operations (union, intersection, difference).
- When order doesn't matter.
- For removing duplicates from a list.
Key Takeaways
- Sets are unordered collections of unique items.
- Use
{}with a type argument to create a set literal.
- Use
- Use
Set.from(),Set.of(),Set.identity(), orSet.unmodifiable()for specialised sets.
- Use
- Add elements with
add()andaddAll().
- Add elements with
- Remove elements with
remove(),removeAll(),retainAll(), andclear().
- Remove elements with
- Perform mathematical operations:
union(),intersection(),difference().
- Perform mathematical operations:
- Iterate with
for‑inorforEach.
- Iterate with
- Sets are perfect for ensuring uniqueness and fast lookups.