Skip to main content
A Set in Dart is a collection of unique items. It implements the Iterable interface and ensures that no two elements within the collection are equal according to the == operator. While the theoretical concept of a set is unordered, Dart’s default implementation (LinkedHashSet) preserves the insertion order of elements during iteration.

Declaration and Initialization

Sets can be initialized using set literals or the Set constructor. Dart infers the type of the Set based on its elements unless explicitly typed.

Set Literals

Set literals use curly braces {}.
// Inferred as Set<String>
var halogens = {'fluorine', 'chlorine', 'bromine', 'iodine', 'astatine'};

// Explicitly typed
Set<int> primeNumbers = {2, 3, 5, 7};

Empty Set Initialization

Because map literals also use curly braces, an empty pair of braces {} defaults to a Map<dynamic, dynamic>, not a Set. To create an empty Set, you must provide a type argument or explicit variable typing.
var names = <String>{}; // Creates an empty Set<String>
Set<String> votes = {}; // Creates an empty Set<String>

var invalidSet = {};    // Creates a Map<dynamic, dynamic>

Uniqueness and Equality

The Set enforces uniqueness using the object’s hashCode and == operator. If an attempt is made to add an element that is already present, the Set remains unchanged, and the operation returns false (specifically for the add method).
var numbers = {1, 2, 3};

// Attempting to add a duplicate
bool added = numbers.add(1); 

print(added);   // false
print(numbers); // {1, 2, 3}

Set Algebra

Dart provides built-in methods to perform mathematical set operations. These methods return a new Set instance containing the result.
final setA = {1, 2, 3, 5};
final setB = {1, 5, 7, 9};

// Union: Elements in A or B (or both)
print(setA.union(setB)); // {1, 2, 3, 5, 7, 9}

// Intersection: Elements in both A and B
print(setA.intersection(setB)); // {1, 5}

// Difference: Elements in A but not in B
print(setA.difference(setB)); // {2, 3}

Element Management

Elements are manipulated via methods defined in the Set class and the Iterable interface. Unlike List, elements in a Set cannot be accessed by an integer index (e.g., set[0] is invalid).
var elements = {'hydrogen', 'helium'};

// Checking existence
print(elements.contains('hydrogen')); // true

// Adding multiple items
elements.addAll(['lithium', 'beryllium']);

// Removing items
elements.remove('helium');

// Iteration
for (final element in elements) {
  print(element);
}

Spread Operators

Dart supports the spread operator (...) and the null-aware spread operator (...?) to insert multiple elements from another collection into a Set.
var setA = {1, 2};
var setB = {3, 4};
var nullableSet;

var combined = {0, ...setA, ...setB};   // {0, 1, 2, 3, 4}
var safeCombine = {0, ...?nullableSet}; // {0}

Conversion

Sets can be created from other iterables using the toSet() method, which effectively deduplicates elements from lists or other collections.
var duplicateList = [1, 2, 2, 3, 1];
var uniqueSet = duplicateList.toSet(); // {1, 2, 3}
var backToList = uniqueSet.toList();   // [1, 2, 3]
Master Dart with Deep Grasping Methodology!Learn More