Exception: Concurrent modification during iteration: Instance(length:17) of ‘_GrowableList’

This error means that you are adding or removing objects from a collection during iteration. This is not allowed since adding or removing items will change the collection size and mess up subsequent iteration.

The error is likely from one of these lines which you haven’t posted the source for:

 s.collisionResponse(e);
 e.collision(s);
 s.collision(e);

I assume one of these calls is removing an item from either the entities list or the rectangles list, during the forEach, which is not allowed. Instead you must restructure the code to remove the items outside of the list iteration, perhaps using the removeWhere method.

Consider the following attempt to remove odd numbers from a list:

var list = [1, 2, 3, 4, 5];
list.forEach( (e) {
 if(e % 2 == 1) 
   list.remove(e);
});

This will fail with a concurrent modification exception since we attempt to remove the items during the list iteration.

However, during the list iteration we can mark items for removal and then remove them in a bulk operation with removeWhere:

var list = [1, 2, 3, 4, 5];
var toRemove = [];

list.forEach( (e) {
 if(e % 2 == 1) 
   toRemove.add(e);
});

list.removeWhere( (e) => toRemove.contains(e));

Leave a Comment