mortezahosseini
mortezahosseini

Reputation: 177

Unable to load asset flutter package

I can load image from assets/images directory in flutter application, but when load same image from assets directory in flutter package, i get this error:

Unable to load asset: assets/images/logo.png

Image provider: AssetImage(bundle: null, name: "assets/images/logo.png") Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#d6b54(), name: "assets/images/logo.png", scale: 1.0)

Upvotes: 0

Views: 1195

Answers (1)

Nico Spencer
Nico Spencer

Reputation: 1180

When loading assets from a package, use the package parameter. Alternative, use the full path to the asset which would look something like packages/<package_name>/assets/images/logo.png.

final image = AssetImage(name: "assets/images/logo.png", package: 'package_name');

Also make sure your asset in your package is referenced in the assets in your package's pubspec.yaml.

flutter:
  assets:
    - assets/images/

Upvotes: 1

Related Questions