Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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');
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading