22

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
  • 4,586
  • 6
  • 32
  • 65

4 Answers4

25

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.

Shahzad Akram
  • 4,586
  • 6
  • 32
  • 65
  • 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 '19 at 08:44
  • I want to read json file from plugin assets can you help me out ? – Sanjeev Sangral Feb 07 '20 at 09: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? – Going Bananas May 18 '21 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 01 '21 at 15:02
21

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.

salihgueler
  • 3,284
  • 2
  • 22
  • 33
  • I got this point but I want them to automatically add when someone installs my plugin. how to accomplish that? – Shahzad Akram Feb 18 '19 at 06:17
  • If you see the quoted information in the edited answer, you can see that, package assets automatically bundled to the application. – salihgueler Feb 18 '19 at 06: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 '19 at 08: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 '20 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 04 '20 at 02:52
  • Thank you! Should be marked as an only correct answer. I spent a couple hours trying to figure out why assets cannot be loaded, until I found your reply about this `package` parameter. In my case, I use a flutter_svg+fluttergen, it looks like this: `Assets.icons.lock.svg(package: 'granat')` – Sergey Molchanovsky Mar 14 '23 at 12:05
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.

allentiology
  • 904
  • 8
  • 7
-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

raman raman
  • 1,655
  • 4
  • 22
  • 34