Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix bug preventing removeAll(children) from be called before mount #3408

Merged
merged 2 commits into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/flame/lib/src/components/core/component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -643,11 +643,13 @@ class Component {

/// Removes all the children in the list and calls [onRemove] for all of them
/// and their children.
void removeAll(Iterable<Component> components) => components.forEach(remove);
void removeAll(Iterable<Component> components) {
components.toList(growable: false).forEach(_removeChild);
}

/// Removes all the children for which the [test] function returns true.
void removeWhere(bool Function(Component component) test) {
removeAll([...children.where(test)]);
children.where(test).toList(growable: false).forEach(_removeChild);
}

void _removeChild(Component child) {
Expand Down
20 changes: 20 additions & 0 deletions packages/flame/test/components/component_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,16 @@ void main() {
await game.ready();
expect(child.isMounted, true);
});

testWithFlameGame(
"can remove component's children before adding the parent",
(game) async {
final c = _ComponentWithChildrenRemoveAll();
game.add(c);

await game.ready();
},
);
});

group('Removing components', () {
Expand Down Expand Up @@ -1804,3 +1814,13 @@ FlameTester<_DetachableFlameGame> _myDetachableGame({required bool open}) {
},
);
}

class _ComponentWithChildrenRemoveAll extends Component {
@override
void onMount() {
super.onMount();

add(Component());
removeAll(children);
}
}
Loading