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.
|
The following Set is not a valid Set, since the element 2
occurred twice.
|
Define Set
We can define a Set variable in Dart using Set
keyword.
|
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.
|
Initialize Set
We can initialize a set by assigning the Set variable with the elements enclosed in curly braces.
|
Or we may create an empty Set using Set()
constructor, and then add the elements using Set.add()
method.
|