Search code examples
flutterimagepackage

Load image in package itself


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?


Solution

  • A flutter clean of both packages helped.