I want to know how this work it should remove the item in list number 3 which is 'four' and print 'one','two','Three' why it is print three, four
here is the code:
final numbers = <String>['one', 'two', 'three', 'four'];
numbers.removeWhere((item) => item.length == 3);
print(numbers); // [three, four]`
CodePudding user response:
removeWhere
:Removes all objects from this list that satisfy test.
An object o satisfies test if test(o) is true.
removeWhere
will go through the list and find the matched item, means where we do our logic and return true.
You can see item.length==3
is true for 'one' and 'two' because its string length is 3, that's why these elements are removed. You can find more on List
You can expand the method like
numbers.removeWhere((item) {
bool isItemContains3Char = item.length == 3;
print("${isItemContains3Char} : $item length : ${item.length}");
return isItemContains3Char;
});
All you need to return true
based on your logic from removeWhere
to remove elements.
CodePudding user response:
final numbers = <String>['one', 'two', 'three', 'four'];
numbers.removeAt(2);
print(numbers);
output
[one, two, four]