Reputation: 5254
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?
Upvotes: 30
Views: 19104
Reputation: 271
If you are, as I am, refactoring large amounts of code into a library, and you need your images to be part of that library, as well as reference those images from within that library, you can sort of follow the instructions in the Flutter docs
Here is my working example. Package pubspec.yaml:
assets:
- lib/assets/
- lib/assets/images/
Example reference within package:
Image.asset("packages/my_library/assets/images/my_image.png")
Package directory structure:
└── my_library
├── lib
│ ├── assets
│ │ ├── my_noise.mp3
│ │ └── images
│ │ └── my_image.png
│ └── library_code
└── pubspec.yaml
Upvotes: 3
Reputation: 5254
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.
Upvotes: 37
Reputation: 1004
Adding Assets to a Flutter Package can get Difficult!
Just follow these steps and you're good to go...
Step 1: Make an assets folder in the root directory of the Package
Step 2: Add it to your pubspec.yaml file
flutter:
assets:
- assets/asset_name
# Mind the indentation
Step 3: Using the asset, for an image file
AssetImage('assets/asset_name.png', package: 'your_package_name')
Run Pub.dev and do a Cold Start for the Flutter Application using the Package and
Upvotes: 0
Reputation: 1811
Pubspec yaml is indentation sensitive
there is a difference between
assets:
- packages/my_plugin/assets/asset_name
VS
assets:
- packages/my_plugin/assets/asset_name
If you closely notice at above two then you will find that pubspec.yaml is very sensitive
always write
assets:
-asset/yourasset/example1
there must be two spaces from the beginning of line.
After adding asset link to pubspec you have to run packages get It will show exits with 0 error if you place your assets properly otherwise it will show you reason behind the error.
Generally, Beginners face these type of problems. With time you will crack the way to solve this error
Upvotes: -3
Reputation: 3622
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.
Upvotes: 25