how can I add assets in flutter pubspec.yaml - flutter

I tried to just add (assets/ ) but that didn't solve the problem

The assets to specify in the pubspec should be inside the flutter section.
flutter:
assets:
- assets/my_icon.png
- assets/background.png
also example with directories:
flutter:
assets:
- directory/
- directory/subdirectory/
Take a fast read to the official docs: https://docs.flutter.dev/development/ui/assets-and-images

Follow below steps
Create a new Folder name "assets" parallel to Lib folder
Create a folder name "images" in it.
Define a assets like a below in a "pubspec.yaml"
assets:
assets/images/
Use it like a below
Image.asset("assets/images/my-profile.png"),

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 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 web app deployed on github.pages cannot access SOME assets

[UPDATED checkout the issue with steps on github ]
running my flutter web app locally
flutter run -d chrome --dart-define=FLUTTER_WEB_USE_SKIA=true --release
works as intended (video), but building it and deploying it to github pages (here)
flutter_master build web --dart-define=FLUTTER_WEB_USE_SKIA=true --release
doesn't access some asset, but successfully access others.
I've tried these solutions (one, two)
'about.json' works as expected locally but fails to load when deployed
while 'assets/about.json' doesn't work in either cases
the code in use can be simplified as
rootBundle.loadString('about.json');
I double-checked pubspec.yaml
flutter:
uses-material-design: true
assets:
- background_portrait.jpg
- background_landscape.jpg
- yf_icon_black.png
- yf_logo.png
- about.json
- apps.json
- news.json
- opensource.json
and the assets in the build folder
everything checks out, but the issue still persists
in these logs you can see that those files are present
What worked for me was to eliminate the assets folder completely. I created a folder for each of my asset types in the root dir (same level as lib) and referenced them as directories in pubspec.yaml:
assets:
- json/
- avatars/
Then when loading them I used a relative path as:
await rootBundle.loadString('json/structure.json');
Flutter creates an assets folder during build and copies all my asset directories into it. This way it worked for me to load the assets both in debug and in release mode on GitLab Pages.
EDIT: I include the gitlab.ci.yml file I use for gitlab pages build pipeline
image: registry.gitlab.com/famedly/containers/flutter-dockerimages:beta
pages:
script:
- flutter clean
- flutter config --enable-web
- flutter pub get
- flutter build web --release
- ls build/web
- cp -r build/web public
- ls public
artifacts:
paths:
- public
only:
- master
The ls commands you do not need these were just for logging the output during development of the script. I left them there because they do no harm and could come handy sometime.
The official flutter documentation explains that the assets should be added relative to the path of pubspec.yaml
For your example of pubspec.yaml, you should either move the asset files in the root of you project folder or if they are located in an assets sub folder add the name of that sub folder in pubspec.yaml
E.g. if your files are located under project_path/assets/, the assets section of your pubspec.yaml should look something like:
flutter:
uses-material-design: true
assets:
- assets/background_portrait.jpg
- assets/background_landscape.jpg
- assets/yf_icon_black.png
- assets/yf_logo.png
- assets/about.json
- assets/apps.json
- assets/news.json
- assets/opensource.json
In your dart code, assets should be accessed by their specified key, for the above example use 'assets/about.json' and not 'about.json'
Its an old thread - just in case someone stumbles upon...you have to ensure that a valid certificate is in place. Otherwise the service worker will not start.

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