fix: Component.loaded future sometimes failed to complete (#1593)

This commit is contained in:
Pasha Stetsenko
2022-05-01 08:04:22 -07:00
committed by GitHub
parent 6dd0a970e6
commit 89ee9b984b
2 changed files with 12 additions and 0 deletions

View File

@@ -501,6 +501,7 @@ class Component {
final onLoadFuture = onLoad(); final onLoadFuture = onLoad();
if (onLoadFuture == null) { if (onLoadFuture == null) {
_state = LifecycleState.loaded; _state = LifecycleState.loaded;
_loadCompleter?.complete();
} else { } else {
return onLoadFuture.then((_) { return onLoadFuture.then((_) {
_state = LifecycleState.loaded; _state = LifecycleState.loaded;

View File

@@ -117,6 +117,17 @@ void main() {
}, },
); );
testWithFlameGame(
'.loaded completes even if accessed before the component added to game',
(game) async {
final component = Component();
final loadedFuture = component.loaded;
game.add(component);
await game.ready();
expectLater(loadedFuture, completes);
},
);
// Obsolete scenario, when we used to have a separate "prepare" stage // Obsolete scenario, when we used to have a separate "prepare" stage
flameGame.test('parent prepares the component', (game) async { flameGame.test('parent prepares the component', (game) async {
final parent = _MyComponent('parent'); final parent = _MyComponent('parent');