Dart has like most other programming languages the Set data structure. A set is an unordered collection of objects where every object can only occure once.

A set can be initialized like the following:

Set mySet = Set();

It also can be initialized with a set literal like:

Set mySet = {};

For more information on literals read the article about literals.

Items can be added to the set by using the add function:


To remove the item from set the remove function can be used:


Values can also be added to the set during initialization using the set literal:

Set mySet = {1,2,3,4};

The Set class implements the Iterable class. A summary of the most common methods can be found in the article about Iterables in Dart.

Generic Type parameter

Set can contain elements of every object. To specify which objects the Set contains the generic type parameter can be set during declaration or initialization.

The following statement will declare a varaible of type Set which contains integers:

Set mySet;

With that the variable mySet will always be a Set which contains integers.

Passing the generic type on initialization looks like:

Set mySet = Set();

Now mySet is as well a Set which contains only integers but the variable itself is of type Set<dynamic>. That will allow us to to the following:

mySet = Set();

This works because the variable itself does not define a generic type. If the generic type is defined on the variable itself it is not possible to assign it to a Set with another generic type.