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:
Note: In Dart, a
setis a particular instance of a list in which all of the inputs are unique.
Note:
dart:coreis 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 theset.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 removeprint("Initial set_1 elements: ${set_1}");//removing element from setprint("Removing 'Eddie' from the set: ${set_1.remove('Eddie')}");//set containing value after removeprint("set_1 elements after removing 'Eddie': ${set_1}");//trying to remove element not present in setprint("Removing 'Jack' from the set: ${set_1.remove('Jack')}");}