kt.dart
kt.dart copied to clipboard
KMutableIterator.remove() doesn't remove item from collection
var abc = mutableListOf(["a", "b", "c"]);
final KMutableIterator<String> i = abc.iterator();
assert(i.next() == "a");
i.remove();
assert(abc[0] == "b"); // Failed assertion: 'abc[0] == "b"': is not true.
abc[0]
is still "a"
because remove doesn't remove the item from the actual collection. Instead, it gets removed from the internal copy inside the iterator.
To fix this problem a big refactoring is required so that remove actually removed the item from the original collection.
This is how kotlin solves this
For the time being, can you suggest any workaround?
I am currently using this, and it's working fine. Don't know if this is the best approach to do.
final list = List<T>.from(KtList<T>);
list.removeWhere((it) => it.id == id);
final updatedList = list.toImmutableList();