flutter unable to load image assets - flutter

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.

Related

Unable to run the flutter application

I get this error when I try running the application.
Error: unable to find directory entry in pubspec.yaml: /Users/yoshithKotla/Desktop/Freewheel_application/assets/images/
# To add assets to your application, add an assets section, like this:
assets:
- assets/images/logo.png
comment out the assets: & make sure to get the path correctly.
You should comment below part from pubspec.yaml if you haven't any directory assets/images in your project.
To add assets to your application, add an assets section, like this:
assets:
- images/a_dot_burr.jpeg
If you have assets and images folder than you should change your pubspec.yaml file like this,
To add assets to your application, add an assets section, like this:
assets:
- assets/images

FileNotFoundError with flutter_dotenv

I installed flutter_dotenv with this command:
flutter pub add flutter_dotenv
My is pubspec.yaml is like:
dependencies:
flutter:
flutter_dotenv: ^5.0.0
flutter:
assets:
- .env
Then I put .env file in the project root (I will use .env.dev. .env.prd later so you see them in the screen shot):
I run project from VSCode then get FileNotFoundError:
I checked the .env file location hundred of times, tried to change the file name, change the location to /lib etc - still no luck. Any idea?
I found what was wrong. "assets:" must be under "flutter:", so tab is necessary before "assets:". That's it!
flutter:
assets:
- .env

Flutter pubspec.yaml error: No file or variants found for asset

my problem is that I am getting an error message saying:
Error detected in pubspec.yaml: No file or variants found for asset: svg/DriverLogoPlain.svg.
My pubspec.yaml:
flutter:
uses-material-design: true
assets:
- svg/DriverLogoPlain.svg
My path to the svg image:
PathImage
Anybody has some to this problem solution please?
Your path in the code is incorrect. You're using assets/svg/DriverLogoOptimized.svg as the path in your Dart code, when it should be svg/DriverLogoOptimized.svg.
This may just be a copy-paste issue, but YAML is also whitespace sensitive, so you can't have flutter: indented so much.
Flutter also doesn't natively support SVGs so beware.
With more info from OP their folder was at the incorrect level. Your assets folder is one level too low. Move it to the same level as android. Not as a subdirectory.

Flutter - No file or variants found for asset: lib/assets/images

I'm getting this error while adding images folder assets in pubspec.yaml
Error detected in pubspec.yaml:
No file or variants found for asset: lib/assets/images.
This is how my pubspec.yaml file looks like
flutter:
uses-material-design: true
assets:
- lib/assets/images
fonts:
- family: Potra
fonts:
- asset: lib/assets/fonts/Potra.ttf
- family: BAHNSCHRIFT
fonts:
- asset: lib/assets/fonts/BAHNSCHRIFT.TTF
I tried to look it up. Most of the people are suggesting to give the right indentation for this problem. But I'm quite sure it is not the case here. I tried the proper indentation method and also all possible variants but in vain.
When I remove below part, code runs fine without any error and respective added font family in pubspec also works fine.
assets:
- lib/assets/images
Why I'm getting error after adding assets part?
This is how my file structure looks like
Your code doesn't work because you need to add one more slash, so it should be like:
assets:
- lib/assets/images/
Tips: It's kinda bad practice if you put your assets folder inside your lib folder. Try to place it outside the lib folder. And also make sure the indentations are correct.
I hope it will be helpful.
You should have your assets folder not inside lib folder but in the main root directory( i.e. out of lib ) and then add assets as :
assets:
# For images
- assets/images
fonts:
- family: FontFamilyName
fonts:
- asset: assets/fonts/the_font_you_want.ttf
And beware of indentation in pubspec.yaml, they also cause errors
I got this exception for a silly mistake I have space in my file name like home_cover. jpg. So I remove it like home_cover.jpg

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.