Dart Sets

Dart Set is a collection of elements, where order of elements is not recorded, and the elements must be unique.

For example, the following Set of integers is a valid Set.

</>
Copy
{2, 4, 6, 0, 1, 8, 9}

The following Set is not a valid Set, since the element 2 occurred twice.

</>
Copy
{2, 4, 2, 0}

Define Set

We can define a Set variable in Dart using Set keyword.

</>
Copy
Set mySet;

We have not specified the type of elements we store in this Set. If type is not specified, then it would be inferred as dynamic.

We may specify the type of elements we store in the Set, as shown in the following.

</>
Copy
Set<int> mySet

Initialize Set

We can initialize a set by assigning the Set variable with the elements enclosed in curly braces.

</>
Copy
Set<int> mySet = {2, 4, 6, 0, 1, 8, 9};

Or we may create an empty Set using Set() constructor, and then add the elements using Set.add() method.

</>
Copy
Set mySet = Set();
mySet.add(2);
mySet.add(4);

Set Tutorials

The following tutorials cover more topics on Sets in Dart programming.

Summary

In this Dart Tutorial, we learned about Sets, and different methods, properties, and concepts related to Sets, with well explained and dedicated tutorials.