How do I access multiple BloCs from a single component in Flutter Flame?

I am building a Flutter Flame game and am trying to have a single action add events to two different blocs. For example, when the character picks up an item I want it to go into an InventoryBloc and increment a counter in GameStatsBloc (for total lifetime items collected).

The documentation examples show things like (copied from here):

class Player extends PositionComponent
    with FlameBlocListenable<PlayerInventoryBloc, PlayerInventoryState> {

  @override
  void onNewState(state) {
    updateGear(state);
  }
}

but explicitly states that you can’t use multiple blocs that way. Similarly, if I try to use multiple FlameBlocListeners they don’t provide access to the relevant blocs to add events (they are listeners after all).

Is there a clean way to have a single component interact with multiple blocs? The only way I can think of doing it is making a dummy parent component that uses FlameBlocListenable<InventoryBloc, InventoryState> that passes the bloc to a child that uses FlameBlocListenable<GameStatsBloc, GameStatsState>, e.g.:

class InventoryProviderComponent extends Component with FlameBlocListenable<InventoryBloc, InventoryState> {
  Future<void> onLoad() async {
    add(Player(inventoryBloc: bloc));
  }
}

class Player extends PositionComponent with FlameBlocListenable<GameStatsBloc, GameStatsState> {
  Player({required this.inventoryBloc});
  final InventoryBloc inventoryBloc;
  ...
  // Can now interact with bloc and inventoryBloc as needed to add events
}

This feels pretty awkward to me and would become increasingly cumbersome if more blocs were needed (though that might indicate other issues with the structure of the code).

Your problem reminds me at Mediator (design pattern)

I recommend you to create an intermediary class, like GameBlocMediator, to handle interactions with BLoCs. Also, since you don’t have access to the BuildContext in onLoad, use the GetIt package for dependency injection.

class GameBlocMediator {
  final InventoryBloc inventoryBloc;
  final GameStatsBloc gameStatsBloc;

  GameBlocMediator({
    required this.inventoryBloc,
    required this.gameStatsBloc,
  });

  void onItemPickedUp(Item item) {
    inventoryBloc.add(AddItemEvent(item)); 
    gameStatsBloc.add(IncrementCounterEvent()); 
  }
}

Create a file like services_locator.dart

final GetIt getIt = GetIt.instance;

void setupServiceLocator() {
  getIt.registerLazySingleton<InventoryBloc>(() => InventoryBloc());
  getIt.registerLazySingleton<GameStatsBloc>(() => GameStatsBloc());
  getIt.registerLazySingleton<GameBlocMediator>(() => GameBlocMediator(
        inventoryBloc: getIt<InventoryBloc>(),
        gameStatsBloc: getIt<GameStatsBloc>(),
      ));
}

Initialize GetIt in main.dart

void main() {
  setupServiceLocator();
...
  runApp(...);
}

You can inject your Mediator in your Player Component

class Player extends PositionComponent {
  final GameBlocMediator mediator = getIt<GameBlocMediator>();

  void pickUpItem(Item item) {
    mediator.onItemPickedUp(item);
  }
}

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật