flame icon indicating copy to clipboard operation
flame copied to clipboard

when using FlameBlocProvider components seem to get created twice

Open tolland opened this issue 2 years ago • 4 comments

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; https://github.com/flame-engine/flame/blob/5b67b8f14ad4fdb38a249d0a41ecba49ba2fcc44/packages/flame_bloc/example/lib/src/game/game.dart#L53-L56

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 avatar Jan 09 '24 10:01 tolland

@erickzanardo does this sound familiar?

spydon avatar Jan 09 '24 10:01 spydon

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.

erickzanardo avatar Jan 09 '24 12:01 erickzanardo

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());
    }
  }

tolland avatar Jan 10 '24 00:01 tolland

Most likely it is because of ide, try running from console. See here: https://github.com/flutter/flutter/issues/33566#issuecomment-884906844

artur-puha-nixs avatar May 17 '24 13:05 artur-puha-nixs

Seems to be unrelated to Flame then; closing (feel free to reopen if I misinterpreted)

luanpotter avatar Jul 04 '24 18:07 luanpotter