I am developing a flutter package containing some assets files. I mentioned required assets in pubsepc.yaml as usual like this
assets:
- assets/abc.xyz
and uploaded the package to https://pub.dartlang.org/.
After that I created a flutter Application and imported my developed package in pubspec.yaml
like...
dependencies:
flutter:
sdk: flutter
my_developed_package: ^0.0.1
Now everything is working fine except my assets are absent.
I put some assets in my Application without mentioning in pubsepc.yaml
and its working. I am unable to understand, how do I add these assets to my package so that they load automatically?
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 is mandatory to put your plugin assets in the lib directory, in other directories it won't work.
It has been added with the plugin and then I accessed them with a path like this
packages/my_plugin/assets/asset_name
, e.g.
File myAsset = File("packages/my_plugin/assets/asset_name");
By this approach, I was able to get an asset from Plugin not only Images.
For a complete example, you can check my plugin here.