Hi Guys, Welcome to Proto Coders Point. In this dart article will learn about list & set in dart & how to convert list to set in dart or vice versa i.e convert set to list in dart.

What is list in dart

Basically dart list is an ordered collecion of data. A dart list look similar to array & the syntax is similar to javascript array literals.

How to declare list in dart

`List lNum = [1, 2, 3, 4, 1, 3, 2];`

In dart list, to store data in it we use bracket [] with data been separated by commas (,).

What is set in dart

A dart set is an unordered collection of data, that means the data stored in sets are unstructured. THe measure difference between list & set is, In sets duplicate values can’t be stored, data store in sets are always unique.

If we try to store duplicate value, set will simply discard the data if data already exist in sets.

syntax to declare set is similar to map in dart

`Set<type> setname = {};`

Example: Set<int> numbers = {1,5,7,9,6,3,4,2};

How to Convert list to set in dart

In dart we have a In-Built method i.e. toSet(). which will help us in converting list to set easily.

Note: As sets stores only unique value, if your list contain any duplicate value, then that duplicate value will not get stored in set for 2 times.

```void main() {
List lNumbers = [1, 2, 3, 4, 1, 3, 2];
List lStrings = ['Rajat', 'Proto', 'Coders', 'Point', 'Flutter', 'Android', 'NodeJS'];

print("\nBefore Converting \n _______________");
print("\n \${lNumbers} ");
print("\n \${lStrings} ");

print("_____________________________________\n ");

var set1Number = lNumbers.toSet();
var set2String = lStrings.toSet();

print("\n After converting list to set \n _______________");
print("\n \${set1Number} ");
print("\n \${set2String} ");
}
```

Output

As you can see in above output, after converting list to set, the set has curly brackers instead of square bracket. And after converting as you see number of element in sets are less than the original element in lists, because the duplicate are discarded.

How to Convert set to list in dart

In dart, There is a in-built functin i.e. toList(), that helps us easily convert set to list in dart.

Example

```void main() {
Set<int> numbers = {1,5,7,9,6,3,4,2};

print("set before converting:: \${numbers} \n");

var list = numbers.toList();

print("set to list converted:: \${list} \n  ");

print("\n ________________________________\n ");

Set<Map<String,dynamic>> person = {
{"name":"Rajat","age": 26},
{"name":"Suraj","age": 30},
{"name":"Pavan","age": 22}
};

print("Set: \${person}");

var personList = person.toList();

print("\nSet to list (Mapped Data): \${personList}");
}
```

Output

So here as you can see in above output converted collection has brackets [], it means set collection is converted in list.

I am a tech geek who likes to contribute to society by continuously spreading his knowledge to you guys, I have Completed my “Master’s of the computer application ( M.C.A )” from Gogte Institute of Technology, Belgaum, I love to share my technical knowledge by writing programming blogs, I even like to use new tech Gadgets. I am interested mostly in Mobile Application Development mostly on Android and currently beginner in Flutter Development.