Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable top load asset from Flutter plugin

Tags:

flutter

I am developing a flutter plugin, and when using the plugin I get Unable to load asset error. Do I have to do something special when using a plugin?

I have no problem loading images in the main application.

From pubspec.yaml:

flutter:
  # To add assets to your plugin package, add an assets section, like this:
  assets:
   - icons/
   - icons/myimage.png # << Just to show, that this also is not not working
  uses-material-design: true
  plugin:
  ...

Also tried:
- moving back and forth with TABs etc.
- renamed the folder to assets

Using the image folder asset:

Image.asset('icons/myimage.png', height: 12.0),

I get this error:

flutter: ══╡ EXCEPTION CAUGHT BY IMAGE RESOURCE SERVICE ╞════════════════════════════════════════════════════
flutter: The following assertion was thrown resolving an image codec:
flutter: Unable to load asset: icons/myimage.png
like image 913
Chris G. Avatar asked Mar 05 '23 10:03

Chris G.


1 Answers

To load assets from a non-application package, you need to pass the package parameter to methods that load the asset like

Image.asset('icons/myimage.png', package: 'my_package', height: 12.0),

See also docs.flutter.io/flutter/widgets/Image/Image.asset.html

To be able to use assets from a dependency (plugin or plain Dart package) follow https://flutter.dev/docs/development/ui/assets-and-images#bundling-of-package-assets

In a dependency all files need to be inside lib/ because only these files are available for package users.

The asset path in pubspec.yaml needs to start with packages/package_name/some_folder_inside_lib

flutter
  assets:
    - packages/my_package/some_folder_inside_lib/my_image.png

Currently there is another limitation, that all asset files need to be listed individually in pubspec.yaml in contrary to assets from the application project where listing the folder is enough. Upvote and subscribe to https://github.com/flutter/flutter/issues/22944 to get notified about updates.

like image 150
Günter Zöchbauer Avatar answered Mar 10 '23 10:03

Günter Zöchbauer