convert list to set in dart flutter
convert list to set in dart flutter

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};


Dart number format - display number... x
Dart number format - display number by adding comma’s.
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

dart list to set example
dart list to set example

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

dart set to list example
dart set to list example

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