Set in Dart

Set In Dart

Set is a unique collection of items. You cannot store duplicate values in the Set. It is unordered, so it can be faster than lists while working with a large amount of data. Set is useful when you need to store unique values without considering the order of the input. E.g., fruits name, months name, days name, etc. It is represented by Curley Braces{}.

Info

Note: The list allows you to add duplicate items, but the Set doesn’t allow it.

Syntax

Set <variable_type> variable_name = {};

How To Create A Set In Dart

You can create a Set in Dart using the Set type annotation. Here Set<String> means only text is allowed in the Set.

void main(){
  Set<String> fruits = {"Apple", "Orange", "Mango"};
  print(fruits);
}

Show Output
Run Online

Set Properties In Dart

Properties Work
first To get first value of Set.
last To get last value of Set.
isEmpty Return true or false.
isNotEmpty Return true or false.
length It returns the length of the Set.

Example of Set Properties Dart

This example finds the first and last element of the Set, checks whether it is empty or not, and finds its length.

void main() {
  // declaring fruits as Set
  Set<String> fruits = {"Apple", "Orange", "Mango", "Banana"};

  // using different properties of Set
  print("First Value is ${fruits.first}");
  print("Last Value is ${fruits.last}");
  print("Is fruits empty? ${fruits.isEmpty}");
  print("Is fruits not empty? ${fruits.isNotEmpty}");
  print("The length of fruits is ${fruits.length}");
}

Show Output
Run Online

Check The Available Value

If you want to see whether the Set contains specific items or not, you can use the contains method, which returns true or false.

void main(){
  Set<String> fruits = {"Apple", "Orange", "Mango"};
  print(fruits.contains("Mango"));
  print(fruits.contains("Lemon"));
}

Show Output
Run Online

Add & Remove Items In Set

Like lists, you can add or remove items in a Set. To add items use add() method and to remove use remove() method.

Method Description
add() Add one element to Set.
remove() Removes one element from Set.
void main(){
 Set<String> fruits = {"Apple", "Orange", "Mango"};
  
  fruits.add("Lemon");
  fruits.add("Grape");
  
  print("After Adding Lemon and Grape: $fruits");
  
  fruits.remove("Apple");
  print("After Removing Apple: $fruits");
}

Show Output
Run Online

Adding Multiple Elements

You can use addAll() method to add multiple elements from the list to Set.

Method Description
addAll() Insert the multiple values to the given Set.
void main(){
 Set<int> numbers = {10, 20, 30};
  numbers.addAll([40,50]);
 print("After adding 40 and 50: $numbers");
}    

Show Output
Run Online

Printing All Values In Set

You can print all Set items by using loops. Click here if you want to learn loop in dart.

void main(){
 Set<String> fruits = {"Apple", "Orange", "Mango"};
  
 for(String fruit in fruits){
   print(fruit);
 }
}

Show Output
Run Online

Set Methods In Dart

Some other helpful Set methods in dart.

Method Description
clear() Removes all elements from the Set.
difference() Creates a new Set with the elements of this that are not in other.
elementAt() Returns the index value of element.
intersection() Find common elements in two sets.

Clear Set In Dart

In this example, you can see how to remove all items from the Set in dart.

void main() {
  Set<String> fruits = {"Apple", "Orange", "Mango"};
  // to clear all items
  fruits.clear();

  print(fruits);
}

Run Online

Difference In Set

In Dart, the difference method creates a new Set with the elements that are not in the other.

void main() {
  Set<String> fruits1 = {"Apple", "Orange", "Mango"};
  Set<String> fruits2 = {"Apple", "Grapes", "Banana"};

  final differenceSet = fruits1.difference(fruits2);

  print(differenceSet);
}

Show Output
Run Online

Element At Method In Dart

In Dart you can find the Set value by its index number. The index number starts with 0.

void main() {
  Set<String> days = {"Sunday", "Monday", "Tuesday"};
  // index starts from 0 so 2 means Tuesday
  print(days.elementAt(2));
}

Run Online

Intersection Method In Dart

In Dart, the intersection method creates a new Set with the common elements in 2 Sets. Here Apple is available in both Sets.

void main() {
  Set<String> fruits1 = {"Apple", "Orange", "Mango"};
  Set<String> fruits2 = {"Apple", "Grapes", "Banana"};

  final intersectionSet = fruits1.intersection(fruits2);

  print(intersectionSet);
}

Run Online

Video

Watch our video on the set in Dart.