SETS IN DART (Flutter)

SETS IN DART (Flutter)

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.

{2, 4, 6, 0, 1, 8, 9}

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

{2, 4, 2, 0}

Define Set

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

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.

Set<int> mySet

Initialize Set

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

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.

Set mySet = Set();

THANK YOU!