Sets

A set is an unordered collection of unique items. Unlike List, Sets doesn't suppoert duplicate values. All the elements are unique.

You can create a set using curly brackets. Here is an example,

var fruits = {'Mango', 'Apple', 'Banana'};

In the above sets we have three elements. Dart automatically detects the type of the element of the Sets and automatically treat the set as appropritate type. For example in the above case, the set fruits has type Set<String>. Now if you try to insert value which is not string, it will result an error.

How to create Empty Set?

To create a empty set, you need to use the following syntax

var names = <String>{};

// or,

Set<String> names = {};

It means you need to specify the Sets type. Otherwise you can never create an empty set, because the following code creates an empty map not an empty set.

var names = {}; // Creates a map, not a set

The reason is, the syntax of a Set and a Map is similar. In both cases, we need to use curly brackets to create them. So by default is set to map. So the empty curly brackets means an empty map. To specify that you are creating empty set, just add the type of data that you are going to use in later time within the set.