18

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?

4 Answers 4

22

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.

4
  • 3
    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.
    – eimmer
    Dec 19, 2019 at 8:44
  • I want to read json file from plugin assets can you help me out ? Feb 7, 2020 at 9:38
  • 1
    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? May 18, 2021 at 10:31
  • 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
    – tmaihoff
    Nov 1, 2021 at 15:02
17

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.

6
  • I got this point but I want them to automatically add when someone installs my plugin. how to accomplish that? Feb 18, 2019 at 6:17
  • If you see the quoted information in the edited answer, you can see that, package assets automatically bundled to the application. Feb 18, 2019 at 6:29
  • 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
    Dec 19, 2019 at 8:38
  • @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.
    – Gábor
    Mar 13, 2020 at 12:06
  • 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.
    – Taha Ali
    May 4, 2020 at 2:52
0

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

Voila You're Done.

1
  • 2
    How would one load non-image assets from a package? e.g. .json or .dat file? May 17, 2021 at 10:26
-2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.