skip to Main Content

I have a "base" package in Flutter where all my shared code lives and I consume it in multiple wrapper packages "app1" etc.

I have a lot of images in the "base" package and I reference them in the pubspec.yaml:

flutter:
  assets:
    - assets/images/

Now, I want to use it directly in the base package, but neither this

Image.asset(
  'packages/base/assets/images/test.png',
)

or this works.

Image.asset(
  'assets/images/test.png',
   package: 'base',
)

Event though the docs state that this should work like that: https://docs.flutter.dev/ui/assets/assets-and-images#from-packages

I always get this error:

════════ Exception caught by image resource service ════════════════════════════
The following assertion was thrown resolving an image codec:
Unable to load asset: "packages/base/assets/images/test.png".

Exception: Asset not found

What do I do wrong?

2

Answers


  1. Chosen as BEST ANSWER

    A flutter clean of both packages helped.


  2. Specify file in pubspec.yaml

    flutter:
      assets:
        - assets/images/test.png
    

    Then use it with package parameter:

    Image.asset(
      'assets/images/test.png',
       package: 'base',
    )
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search