Skip to content

Commit

Permalink
fix: Materialize list in Component.removeWhere (#2458)
Browse files Browse the repository at this point in the history
This is needed since it'll get a ConcurrentModificationException in some cases otherwise, like in the test added for example.
  • Loading branch information
spydon committed Apr 2, 2023
1 parent d1ac01f commit 13cce4a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
6 changes: 1 addition & 5 deletions packages/flame/lib/src/components/core/component.dart
Expand Up @@ -614,11 +614,7 @@ class Component {

/// Removes all the children for which the [test] function returns true.
void removeWhere(bool Function(Component component) test) {
for (final component in children) {
if (test(component)) {
remove(component);
}
}
removeAll([...children.where(test)]);
}

void _removeChild(Component child) {
Expand Down
21 changes: 21 additions & 0 deletions packages/flame/test/components/component_test.dart
Expand Up @@ -691,6 +691,19 @@ void main() {
);
},
);

testWithFlameGame(
'removeWhere works before all components are mounted',
(game) async {
game.add(_RemoveWhereComponent());
expect(
() async {
await game.ready();
},
returnsNormally,
);
},
);
});

group('Moving components', () {
Expand Down Expand Up @@ -1302,3 +1315,11 @@ class _OnChildrenChangedComponent extends PositionComponent {
lastChangeType = type;
}
}

class _RemoveWhereComponent extends Component {
@override
Future<void> onLoad() async {
add(Component());
removeWhere((_) => true);
}
}

0 comments on commit 13cce4a

Please sign in to comment.