What is the set.remove() method in Dart?

The set.remove() function in Dart is used to remove a specific element from a set.

The image below shows a visual representation of the set.remove() function:

Visual representation of the "set.remove()" function

Note: In Dart, a set is a particular instance of a list in which all of the inputs are unique.

Note: dart:core is required in order to use this function.


Note: If there are duplicates, then the set will only keep the first instance. For example, {'Tom','Alsvin','Eddie','Tom'} will result in {'Tom','Alsvin','Eddie'}, after the set.remove() function is called.

Syntax

bool set_name.remove(element)
// where the set_name is the name of the set.

Parameter

This function takes the element that is to be removed as a parameter.

Return value

This function removes the element that is sent as a parameter from the set. It returns True if the element is present in the set. Otherwise, it returns False.

Code

The code given below shows us how to use the set.remove() function in Dart:

import 'dart:convert';
import 'dart:core';
void main() {
Set<String> set_1 = {'Tom','Alsvin','Eddie'};
//set containing value before remove
print("Initial set_1 elements: ${set_1}");
//removing element from set
print("Removing 'Eddie' from the set: ${set_1.remove('Eddie')}");
//set containing value after remove
print("set_1 elements after removing 'Eddie': ${set_1}");
//trying to remove element not present in set
print("Removing 'Jack' from the set: ${set_1.remove('Jack')}");
}

Free Resources