For example,
Lets say you have a list of apples
List<String> apples = ['green', 'red','red', 'yellow']
And you want to remove just one red apple using .removeWhere
apples.removeWhere((element => element == 'red));
however, this removes all instances of ‘red’ when I only want to remove one of them! Any solutions?
3
Answers
you could use directly the
remove
method on your list:And if you want to use it like you’re using the
removeWhere
method, you can use this extension method :Add this on the global scope (outside your classes, just put it anywhere else you want)
Then you can use it like this:
A third solution, if you want to remove all occurrences in your
List
so everyString
should be there only once, you can simply make it aSet
: