How to add assets in flutter Package/Plugin development? How to add assets in flutter Package/Plugin development? dart dart

How to add assets in flutter Package/Plugin development?


Quote from

If the desired asset is specified in the pubspec.yaml file of the package, it is bundled automatically with the application. In particular, assets used by the package itself must be specified in its pubspec.yaml.

In Flutter you can use assets from packages, it should not be a problem. Only thing is, you need to specify your package and import it. E.g. If it's an image, you can use AssetImage class and it's package attribute.

AssetImage('assets/abc.xyz', package: 'my_developed_package');

For more information about how you can call texts and other stuff, please check here.


Sorry for the late answer but the following approach helped me for including assets (not only images but any type of file) in plugin development.

I put my assets under the lib folder like, my_plugin/lib/assets and in pubspec.yaml like this.

  assets:    - packages/my_plugin/assets/asset_name         # Be careful about indentation

It has been added with the plugin and then I accessed them with path like thispackages/my_plugin/assets/asset_name, e.g.

File myAsset = File("packages/my_plugin/assets/asset_name");

By this approach, I was able to get any asset from Plugin not only Images.

For a complete Example, you can check my plugin here.


To load an image from a package dependency, the package argument must be provided to AssetImage.

For instance, suppose your application depends on a package called my_icons, which has the following directory structure:

.../pubspec.yaml.../icons/heart.png.../icons/1.5x/heart.png.../icons/2.0x/heart.png...etc.

To load the image, use:

AssetImage('icons/heart.png', package: 'my_icons')

Assets used by the package itself should also be fetched using the package argument as above.