Documentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
A Set in Dart is an iterable collection of unique objects. It enforces element uniqueness by evaluating the equality of its items using their == operator and hashCode properties. Attempting to add a duplicate value to a Set is a no-op; the collection remains unchanged and no exception is thrown.
By default, Dart set literals instantiate a LinkedHashSet, which maintains insertion order while providing amortized constant-time O(1) complexity for add, remove, and lookup operations.
Syntax and Initialization
Sets are typically created using set literals {} or explicitly typed constructors. Because Dart uses {} for both sets and maps, creating an empty set requires explicit type annotations to prevent the compiler from inferring a Map<dynamic, dynamic>.
// Set literal with type inference
var primaryColors = {'red', 'green', 'blue'}; // Inferred as Set<String>
// Explicitly typed Set literal
Set<int> fibonacci = {1, 2, 3, 5, 8};
// Creating an empty Set
var emptySet = <String>{};
Set<double> anotherEmptySet = {};
// Anti-pattern: This creates a Map, not a Set
var notASet = {};
Core Operations
The Set API provides standard collection mutations and lookups.
var nodes = <String>{'A', 'B'};
// Addition
nodes.add('C'); // Returns true (added)
nodes.add('A'); // Returns false (duplicate, ignored)
nodes.addAll({'D', 'E'}); // Adds multiple elements
// Removal
nodes.remove('B'); // Returns true if element existed and was removed
nodes.clear(); // Empties the set
// Lookup
bool exists = nodes.contains('C');
Mathematical Set Operations
Dart provides built-in methods for standard mathematical set theory operations, returning new Set instances without mutating the originals.
var setA = {1, 2, 3, 4};
var setB = {3, 4, 5, 6};
// Union: Combines all unique elements from both sets
// Result: {1, 2, 3, 4, 5, 6}
var union = setA.union(setB);
// Intersection: Yields elements present in both sets
// Result: {3, 4}
var intersection = setA.intersection(setB);
// Difference: Yields elements in setA that are not in setB
// Result: {1, 2}
var difference = setA.difference(setB);
Alternative Implementations
While LinkedHashSet is the default, the dart:collection library provides alternative implementations with different performance and ordering guarantees:
import 'dart:collection';
// HashSet: Unordered. Iteration order is not guaranteed.
// Marginally faster than LinkedHashSet as it doesn't maintain pointers for insertion order.
var hashSet = HashSet<int>();
// SplayTreeSet: A self-balancing binary search tree.
// Elements are iterated in sorted order according to their `compareTo` method
// or a provided Comparator. Operations are O(log n).
var treeSet = SplayTreeSet<int>();
Spread Operator and Control Flow
Sets fully support Dart’s spread operator (...), null-aware spread operator (...?), and collection if/for constructs for declarative composition.
var baseConfig = {'host', 'port'};
bool includeAuth = true;
var finalConfig = <String>{
...baseConfig,
if (includeAuth) 'credentials',
for (var i = 1; i <= 3; i++) 'node_$i'
};
// Result: {'host', 'port', 'credentials', 'node_1', 'node_2', 'node_3'}
Master Dart with Deep Grasping Methodology!Learn More