skip to Main Content

I’m developing a game using the Flutter Flame engine and have packaged the game to be used in another Flutter app. However, I’m encountering an issue where the game, when used as a package dependency in the host app, cannot find its image assets.

My Flame game has its assets located in assets/images/ and they are properly listed in the pubspec.yaml of the game package. It loads the image like this:

spriteImage = await Flame.images.load('abc.png');

I’ve included the game package as a dependency in my host app’s pubspec.yaml. Despite the assets being accessible within the game package, when I run the host app, it stops with exception:

Unable to load asset: "assets/images/abc.png"`

How can I ensure that my Flutter Flame game package can correctly load its assets when used as a dependency in another Flutter app?

Is there in Flame something like the package parameter in the Flutter:

Image.asset('assets/images/abc.png', package: 'my_game')

2

Answers


  1. Chosen as BEST ANSWER

    Found it out. The answer from @Hamed pointed me in the right direction. I only had to adjust the prefix of the flame image cache:

    class MyGame extends FlameGame with ... {
    
      @override
      Future<void> onLoad() async {
        images.prefix = 'packages/ma_package/assets/images/'; // <- added this line
       
    ...
    
      }   
    }
    

  2. Here is the implementation for Image.asset:

    package == null ? assetName : 'packages/$package/$assetName';
    

    Therefore, you can prefix your assets with packages/your_package_name/:

    spriteImage = await Flame.images.load(
      await rootBundle.load(
        'packages/your_package_name/assets/images/abc.png',
      )
    );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search