Unable to load asset flutter package - flutter

I can load image from assets/images directory in flutter application, but when load same image from assets directory in flutter package, i get this error:
Unable to load asset: assets/images/logo.png
Image provider: AssetImage(bundle: null, name: "assets/images/logo.png")
Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#d6b54(), name: "assets/images/logo.png", scale: 1.0)

When loading assets from a package, use the package parameter. Alternative, use the full path to the asset which would look something like packages/<package_name>/assets/images/logo.png.
final image = AssetImage(name: "assets/images/logo.png", package: 'package_name');
Also make sure your asset in your package is referenced in the assets in your package's pubspec.yaml.
flutter:
assets:
- assets/images/

Related

flutter unable to load image assets

this how I called image in my app
Image.asset('assets/user.jpg')
this is my pubspec.yaml
assets:
-assets/user.jpg
and I get this error
Exception has occurred. FlutterError (Unable to load asset: assets/user.jpg)
You must consider indentation for assets in pubspec.yaml
assets:
- assets/user.jpg
Your indentation is not correct. It should be like this:
assets:
- assets/user.jpg
Make sure user.jpg is placed in assets folder in your project's
main directory (not inside lib folder)
After doing this, make sure you rebuild your app to see these changes.

Unable to load asset:

Here is the error massage:
Unable to load asset: assets/images/waiting.png
When the exception was thrown, this was the stack Image provider:
AssetImage(bundle: null, name: "assets/images/waiting.png") Image key:
AssetBundleImageKey(bundle: PlatformAssetBundle#e3e67(), name:
"assets/images/waiting.png", scale: 1.0)
here is my pubspec.yaml
flutter:
uses-material-design: true
# To add assets to your application, add an assets section, like this:
assets:
- assets/images/waiting.png
Make sure that the assets folder is at the level of the lib folder of the project, and start the app again, that is, if you add an image, do not use hot reload, Restart the app the first time you add the image.

Flutter cant to load asset

I cannot load my assets images and icons.
What can be the problem, i cant upgrade flutter version because its throw many errors if its supposed to be answer.
BottomNavigationBarItem(
icon: ImageIcon(
AssetImage("assets/icons/cash-back.png"),
color: Color(0xFF3A5A98),
),
label: 'leagues',
backgroundColor: AppColors.white,
),
error:
════════ Exception caught by image resource service ════════════════════════════
The following assertion was thrown resolving an image codec:
Unable to load asset: assets/icons/cash-back.png
When the exception was thrown, this was the stack
#0 PlatformAssetBundle.load
package:flutter/…/services/asset_bundle.dart:227
<asynchronous suspension>
#1 AssetBundleImageProvider._loadAsync
package:flutter/…/painting/image_provider.dart:673
<asynchronous suspension>
Image provider: AssetImage(bundle: null, name: "assets/icons/cash-back.png")
Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#c10e3(), name: "assets/icons/cash-back.png", scale: 1.0)
In your image, look at where you have specified the assets.
flutter:
#some comments
assets:
- assets/
Notice how you have put "assets:" and "flutter:" on the same indentation. In pubspec.yaml, whitespace is important. Make sure that your indentation looks like this:
flutter:
#some comments
assets:
- assets/
The comments can make this mistake easy to miss, so make sure you pay very close attention to your indentation. Each child should be one tab or two spaces further indented than its parent.
To include all assets under a directory, specify the directory name with the / character at the end and save all data in specific directory ( All images in images folder )
assets:
- assets/images/
- assets/videos/
and run pub get
After pub get success you can refer your assets like
Image.asset(
'assets/images/trash.png',
scale: 1.8,
),
include this in your pubspec.yaml file,
flutter:
assets:
- assets/
- assets/icons/
For more check this link: Adding assets and images
Try this in your .yaml file
flutter:
assets:
-android/assests/directory/

Font from a package (library) not showing up in Flutter app?

I have a package named Handwriter. It writes text in a custom font. I have the .ttf saved in lib/third_party/. In its pubspec.yaml, I add the font:
flutter:
uses-material-design: true
fonts:
- family: FancyHandwriting
fonts:
- asset: lib/third_party/FancyHandwriting-Regular.ttf
My app imports this package. In its pubspec.yaml, I add Handwriter as a dependency:
dependencies:
flutter:
sdk: flutter
handwriter:
path: ../handwriter
model:
path: ../model
However, the font does not show up at all when I use it in my app. Why?
final defaultStyle = TextStyle(
fontFamily: 'FancyHandwriting',
fontSize: 130);
Without having to re-add the fonts in the package consumer app, you can use:
final defaultStyle = TextStyle(
fontFamily: 'FancyHandwriting',
package: 'handwriter',
fontSize: 130,
);
Which is also equivalent to doing:
final defaultStyle = TextStyle(
fontFamily: 'packages/handwriter/FancyHandwriting',
fontSize: 130,
);
To use your Flutter Icon font from a custom package, you can follow these steps:
create a flutter icon font
You can use flutter icon for that https://www.fluttericon.com
Put the file into your package
Make sure to copy the .ttf file into of the /lib folder.
NOT just assets, as you would do in your root project.
Example path:
/packages/my_awesome_fontpackage/lib/assets/MyIconFont.ttf
(see https://zubairehman.medium.com/how-to-use-custom-fonts-images-in-flutter-package-c2d9d4bfd47a )
Add the font to your project
Now open your package's pubspec.yaml file and add the font as an asset with package path:
flutter:
fonts:
- family: MyIconFont
fonts:
- asset: packages/my_awesome_fontpackage/assets/MyIconFont.ttf
(you might have to restart your app and bundling completely for the font to be loaded correctly)
Now add package declaration to font dart file
Go into the my_icon_font.dart file and change the constant _kFontPkg there to your package name.
class MyIconFont {
MyIconFont._();
static const _kFontFam = 'MyIconFont';
static const String? _kFontPkg = 'my_awesome_fontpackage';
static const IconData bell = ...
....
}
According to this: https://flutter.dev/docs/cookbook/design/package-fonts you have to declare the package's font in your app's pubspec.yaml:
Declare the font assets
Now that you’ve imported the package, tell
Flutter where to find the fonts from the awesome_package.
To declare package fonts, prefix the path to the font with
packages/awesome_package. This tells Flutter to look in the lib folder
of the package for the font.
I.e.:
dependencies:
flutter:
sdk: flutter
handwriter:
path: ../handwriter
model:
path: ../model
flutter:
uses-material-design: true
fonts:
- family: FancyHandwriting
fonts:
- asset: packages/handwriter/third_party/FancyHandwriting-Regular.ttf
Note you're declaring the asset as being from the package - you don't have to make a copy of it in your app's lib.
The reasoning behind this is that not all fonts from every package may be used, so this trims down your final app's size.
Don't forget that you must also import/copy the fonts into your font assets directory. In your case make sure you place them in lib/third_party/. (See step 1 - https://flutter.dev/docs/cookbook/design/fonts).

Unable top load asset from Flutter plugin

I am developing a flutter plugin, and when using the plugin I get Unable to load asset error. Do I have to do something special when using a plugin?
I have no problem loading images in the main application.
From pubspec.yaml:
flutter:
# To add assets to your plugin package, add an assets section, like this:
assets:
- icons/
- icons/myimage.png # << Just to show, that this also is not not working
uses-material-design: true
plugin:
...
Also tried:
- moving back and forth with TABs etc.
- renamed the folder to assets
Using the image folder asset:
Image.asset('icons/myimage.png', height: 12.0),
I get this error:
flutter: ══╡ EXCEPTION CAUGHT BY IMAGE RESOURCE SERVICE ╞════════════════════════════════════════════════════
flutter: The following assertion was thrown resolving an image codec:
flutter: Unable to load asset: icons/myimage.png
To load assets from a non-application package, you need to pass the package parameter to methods that load the asset like
Image.asset('icons/myimage.png', package: 'my_package', height: 12.0),
See also docs.flutter.io/flutter/widgets/Image/Image.asset.html
To be able to use assets from a dependency (plugin or plain Dart package) follow https://flutter.dev/docs/development/ui/assets-and-images#bundling-of-package-assets
In a dependency all files need to be inside lib/ because only these files are available for package users.
The asset path in pubspec.yaml needs to start with packages/package_name/some_folder_inside_lib
flutter
assets:
- packages/my_package/some_folder_inside_lib/my_image.png
Currently there is another limitation, that all asset files need to be listed individually in pubspec.yaml in contrary to assets from the application project where listing the folder is enough.
Upvote and subscribe to https://github.com/flutter/flutter/issues/22944 to get notified about updates.