Đăng Huân
Đăng Huân

Reputation: 159

Flutter - Asset images in package dependencies

We have the following structure: Package A is a flutter app, It has it's assets folder and is loading assets like this: Image.asset("assets/images/background.png"). project B, that uses package A as dependency.

When I run package A from Project B, some widget of package A have image can not show. Error: Another exception was thrown: Unable to load asset: assets/images/light_theme_background.svg

When I added the "package" field to the AssetImage in the package A, Error is fixed when I run package A from project B, however the package A couldn't read its own contents.

const AssetImage('assets/heart.png', package: 'my_packages');

Is there a way to package A when it can read assets on run from itself and run from the project that takes it as a dependency?

Error when run package A: "Another exception was thrown: Unable to load assets: package/my_packages/assets/images/background.png"

In the documentation it says: "Content used by the package itself should also be fetched using the package argument as above."

I tried copying assets folder to lib folder and also declaring in pubspec.yaml file like the answer here. However, most reported "Bad state: Invalid SVG data " and "Exception: Invalid image data" errors even though they are all normal.

Upvotes: 6

Views: 1214

Answers (1)

Buhaescu Vlad
Buhaescu Vlad

Reputation: 88

I might have a solution using flutter_gen package. I managed to strip it down to not having this structure in the pubspec.yaml file of the dependency package, in your case package A:

flutter_gen:
  assets:
    outputs:
      package_parameter_enabled: true

After adding this I was able to see my pdf file. [that was my use case]

Upvotes: 1

Related Questions