How to add assets in flutter Package/Plugin development?

6,912

Solution 1

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.

Solution 2

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.

Solution 3

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.

Share:
6,912
Shahzad Akram
Author by

Shahzad Akram

Updated on December 09, 2022

Comments

  • Shahzad Akram
    Shahzad Akram over 1 year

    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?

  • Shahzad Akram
    Shahzad Akram about 5 years
    I got this point but I want them to automatically add when someone installs my plugin. how to accomplish that?
  • salihgueler
    salihgueler about 5 years
    If you see the quoted information in the edited answer, you can see that, package assets automatically bundled to the application.
  • eimmer
    eimmer over 4 years
    Only AssetImage allows you to specify a package. If you want to load a json file it doesn't work, and I find the documentation very difficult to understand on this. It states you can have assets not defined in the pubspec.yaml, but in the next sentence still says you have to define them in the pubspec.yaml.
  • eimmer
    eimmer over 4 years
    This is a winner, thank you! I spent all morning trying to get my package to load it's own JSON file, and found the flutter documentation a little.....unclear.
  • Sanjeev Sangral
    Sanjeev Sangral over 4 years
    I want to read json file from plugin assets can you help me out ?
  • Gábor
    Gábor about 4 years
    @eimmer - you can have several assets and you can specify some but not necessarily all of them to be available for users of the package. The rest will be for your package's own use but not exposed to the outside world.
  • Taha Ali
    Taha Ali about 4 years
    Thank you so much for this answer. Have been looking for hours. Only if I can choose this is as the best answer because the answer below is a bit arduous task to achieve same thing.
  • Going Bananas
    Going Bananas about 3 years
    How would one load non-image assets from a package? e.g. .json or .dat file?
  • Going Bananas
    Going Bananas about 3 years
    This works but I have to include every single asset file separately in the application's pubspec.yaml under assets:. Is there any way to include all assets under a package folder too like one can do for normal assets in an application?
  • tmaihoff
    tmaihoff over 2 years
    Thanks for that! I can confirm that this works. I specified the txt file in the pubspec.yaml` of the package project and accessed it via rootBundle.loadString('packages/package_name/file.txt). Note that the file is in package_name/lib/file.txt. No need to specify the lib folder