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

when using FlameBlocProvider components seem to get created twice #2967

Open
tolland opened this issue Jan 9, 2024 · 4 comments
Open

when using FlameBlocProvider components seem to get created twice #2967

tolland opened this issue Jan 9, 2024 · 4 comments
Labels

Comments

@tolland
Copy link

tolland commented Jan 9, 2024

Current bug behavior

I noticed that states are being emitted twice in the flame_bloc game demo here:
https://github.com/flame-engine/flame/blob/main/packages/flame_bloc/example/lib/src/game/game.dart

seems to be the result of these children getting added twice;

children: [
player = PlayerComponent(),
PlayerController(),
GameStatsController(),

Expected behavior

only add the components to the tree once.

Steps to reproduce

I added some debugging to show the effect:
https://github.com/flame-engine/flame/compare/main...tolland:flame:double-added-bloc?expand=1

This shows that they are being added twice:

Performing hot restart...
Waiting for connection from debug service on Chrome...
Restarted application in 88ms.
creating PlayerComponent - 686141183
creating player controller - 142910962
creating game stats controller - 523610366
creating PlayerComponent - 776595899
creating player controller - 1023012259
creating game stats controller - 692494327

same sort of thing on died/respawned

in PlayerDied in bloc handler
player respawned on GameStatus.respawn - 142910962
creating PlayerComponent - 290429235
player respawned on GameStatus.respawn - 1023012259
creating PlayerComponent - 510917524
in PlayerRespawned in bloc handler
in PlayerRespawned in bloc handler

Flutter doctor output

[example] $ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.16.5, on Fedora Linux 38 (Workstation Edition) 6.6.9-100.fc38.x86_64, locale
    en_GB.UTF-8)
[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[✓] Chrome - develop for the web
[✓] Linux toolchain - develop for Linux desktop
[✓] Android Studio (version 2023.1)
[✓] IntelliJ IDEA Ultimate Edition (version 2022.2)
[✓] IntelliJ IDEA Community Edition (version 2023.3)
[✓] VS Code (version 1.85.1)
[✓] Connected device (3 available)
[✓] Network resources

• No issues found!

More environment information

Log information

Enter log information in this code block

More information

@tolland tolland added the bug label Jan 9, 2024
@spydon
Copy link
Member

spydon commented Jan 9, 2024

@erickzanardo does this sound familiar?

@erickzanardo
Copy link
Member

It does not. I have taken a look on the source to check if I could find any obvious reason on why this is happening, but I could not spot anything. Would need to do some debugging to understand.

@tolland
Copy link
Author

tolland commented Jan 10, 2024

it seems that the build method is called twice (on the chrome web platform I am testing with) during the loading;

https://github.com/flame-engine/flame/blob/main/packages/flame_bloc/example/lib/src/game.dart#L52-L59

  @override
  Widget build(BuildContext context) {
    return GameWidget(
      game: SpaceShooterGame(
        statsBloc: context.read<GameStatsBloc>(),
        inventoryBloc: context.read<InventoryBloc>(),
      ),
    );
  }

resulting in 2 FlameGames, the first one being removed, but the first still has the children (e.g. PlayerController) with the listeners:
https://github.com/flame-engine/flame/blob/main/packages/flame_bloc/example/lib/src/game/components/player.dart#L22-L27

  void onNewState(GameStatsState state) {
    if (state.status == GameStatus.respawn ||
        state.status == GameStatus.initial) {
      game.statsBloc.add(const PlayerRespawned());
      parent?.add(game.player = PlayerComponent());
    }
  }

@arthurpuha
Copy link

Most likely it is because of ide, try running from console. See here: flutter/flutter#33566 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants