flutterdart

Unable to load asset with package structure


My project uses a package structure using melos. I have a package ui_components which includes ui kit and assets. Assets contains folders with images for different features: game, icons, portfolio, etc.

I also have the package game. It takes images from ui_components/assets/game. But some images are not loaded:

Another exception was thrown: Unable to load asset: "assets/game/idv_short.webp".

Dir ui_components/assets/game is new. If I move the images from games to other pre-existing folders, such as portfolio, then the images load fine. In the ui_components package in pubspec.yaml I specified:

flutter:
  uses-material-design: true
  assets:
    ...
    - assets/game/

In the game package I imported:

ui_components: 0.0.1

In packages/ui_components/lib/generated/assets.gen.dart all fine:

  /// File path: assets/game/idv_short.webp
  AssetGenImage get idvShort => 
      const AssetGenImage('assets/game/idv_short.webp');

How I use it:

BoxDecoration(
    image: DecorationImage(
        image: AssetImage(Assets.weeksGame.idvShort.path),
            fit: BoxFit.cover,
     ),
 ),

What am I missing?


Solution

  • Try adding your assets this way:

    pubspec.yaml

      assets:
        - assets/images/ic_app_logo.png
        - assets/images/ic_app_logo_new.png
        - assets/images/ic_attachment.png
        - assets/images/ic_close.png
    

    widget.dart

                           Container(
                          child: const Image(
                              fit: BoxFit.contain,
                              image:
                                  AssetImage('assets/images/ic_app_logo.png')),
                        ),
    

    and To load an image from a package(ui_components) dependency, the package argument must be provided to AssetImage.

     const AssetImage('assets/game/ic_app_logo.png', package: 'ui_components');