To remove a specific item from a set in Dart, you can use the remove method to delete the item from the set.
In this example,
numberSet
using the {}
syntax, which contains integer values 1
, 2
, 3
, 4
, and 5
.remove
method of numberSet
to remove the item 3
from the set.numberSet
to see the removed item.void main() {
Set<int> numberSet = {1, 2, 3, 4, 5};
numberSet.remove(3);
print(numberSet);
}
{1, 2, 4, 5}
In this example,
stringSet
using the {}
syntax, which contains string values 'apple'
, 'banana'
, 'cherry'
, 'date'
, and 'grape'
.remove
method of stringSet
to remove the item 'cherry'
from the set.stringSet
to see the removed item.void main() {
Set<String> stringSet = {'apple', 'banana', 'cherry', 'date', 'grape'};
stringSet.remove('cherry');
print(stringSet);
}
{'apple', 'banana', 'date', 'grape'}
In this tutorial, we learned How to Remove Specific Item from a Set in Dart language with well detailed examples.