Reputation: 1709
I have an module with an 'assets' folder located in the same directory as my pubspec.yaml file. In my assets folder I have test.txt, and simpleObject.json.
flutter:
assets:
- assets/test.txt
- assets/simpleObject.json
I believe the following code should then allow me to read it into my app.
var test = await DefaultAssetBundle.of(context).loadString("assets/test.txt");
Sadly I get the following error:
[ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: Unable to load asset: assets/test.txt
The error comes from the asset_bundle.dart. I have to assume this is my fault, but according to everything I've read I'm doing it correctly. Any thoughts?
Here is my file structure if it helps.
MyModule
|_assets/test.txt
|_lib/
|_pubspec.yaml
Upvotes: 15
Views: 5496
Reputation: 715
This is a bit late, but another solution can be:
Image.asset('packages/<package_name>/assets/splash.png');
Upvotes: 14
Reputation: 1709
I got a solution. Even though my package was trying to load its own asset, it still had to specify itself as the location.
Here is how my_package loads an image asset (specifying the package), uses it in a widget, and that widget is easily used by outside apps.
Image.asset(AssetImage("assets/splash.png").assetName, package: 'my_package',);
Upvotes: 23
Reputation: 21
I encountered this problem and took a lot of time before it was resolved. The solution was to downgrade to a previous version (git checkout v1.5.4-hotfix.2)
Upvotes: 0