flutter_svg icon indicating copy to clipboard operation
flutter_svg copied to clipboard

Unable to load asset ~ The asset does not exist or has empty data (even though the file is there)

Open KiraResari opened this issue 2 years ago • 19 comments

When trying to load an svg file like this:

SvgPicture.asset("assets/images/MyIcon.svg")

...I get this error:

E/flutter (31093): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Unable to load asset: "assets/images/MyIcon.svg".
E/flutter (31093): The asset does not exist or has empty data.
E/flutter (31093): #0      PlatformAssetBundle.load.<anonymous closure> (package:flutter/src/services/asset_bundle.dart:254:9)

I confirmed that the icon is there, and that it is not empty. In fact, originally I did convert the svg into a png, which is placed in the same folder, and working it like this works fine:

Image.asset("assets/images/MyIcon.png")

I wanted to use the svg version though to be able to make the most of the vector-nature of that icon, to be able to scale it and change the color. But apparently, it does not work.

My project structure looks roughly like this:

📁MyProject
├📁android
├📁assets
│└📁images
│ ├📄MyIcon.png
│ └📄MyIcon.svg
┊

The MyIcon.png was converted from the MyIcon.svg, which is thus definitely a falid svg file and not empty.

And this works:

Image.asset("assets/images/MyIcon.png")

But this does not:

SvgPicture.asset("assets/images/MyIcon.svg")

Am I doing something wrong here? Or is there something wrong with this library?

KiraResari avatar Aug 08 '23 11:08 KiraResari

define it in pubspec.yaml... or ask ChatGPT, it tells you stright away..

hansheng654 avatar Aug 17 '23 12:08 hansheng654

Same here. Have you found any solutions?

H1eu69 avatar Sep 06 '23 07:09 H1eu69

Same here. Have you found any solutions?

I had such a problem. I got into pubspec.yaml and made the correct space for assets. Was: image Now: image

DinoDafor avatar Oct 12 '23 10:10 DinoDafor

hi i can help you in pubscec.yaml, please do this:

assets: - images/1.svg

you MUST write full name of your svg file

if you do this, you will have errore:

assets: - images/

but when you write 1.svg after your asset folder, it will be OK

MohammadRezaRaad avatar Dec 18 '23 14:12 MohammadRezaRaad

H, I'm currently experiencing the same issue. It loads when run on the web and on windows, but for some reson unknown to me, it is unable to find the path when i run it on my android emulator. Android:- android

Windows:- windows

Web:- web

Dolapo-A avatar Jan 15 '24 12:01 Dolapo-A

i have same problem on android (on windows works fine). any solution or update?

Ali-Fadaei avatar Jan 15 '24 19:01 Ali-Fadaei

I've been able to solve it, turns out the cause of the issue was the underscore in the file path, after renaming the directory from navbar_components to components.

Dolapo-A avatar Jan 16 '24 09:01 Dolapo-A

i have same problem on android (on windows works fine). any solution or update?

what does your path look like in pubspec.yaml

Dolapo-A avatar Jan 16 '24 22:01 Dolapo-A

i have same problem on android (on windows works fine). any solution or update?

what does your path look like in pubspec.yaml

assets: - assets/logo/ - assets/animation/ - assets/icons/broken/ - assets/icons/broken-color/ - assets/icons/filled/ - assets/icons/filled-color/ - assets/images/auth/

Ali-Fadaei avatar Jan 17 '24 19:01 Ali-Fadaei

In my case i was trying to get the get the asset from another package that belongs to the workspace, i was sure the pub was ok but still getting that error the solution its to specify the package

SvgPicture.asset(
        'assets/svg/apple-logo.svg',
        height: size,
        package: 'panorama_design_system', //HEREEE
      ),

cristiancamilo070 avatar Feb 23 '24 00:02 cristiancamilo070

same problem here it says I/flutter (29378): Error playing audio: Unable to load asset: "assets/audio/maayong_adlaw.wav". I/flutter (29378): The asset does not exist or has empty data.

my code onPressed: () async { String translatedText = _translatedText.trim(); String? audioFileName = audioFiles[translatedText]; if (audioFileName != null) { try { await _audioPlayer.stop(); // Stop any currently playing audio await _audioPlayer.play(AssetSource('audio/$audioFileName'),); // Use correct asset path } catch (e) { print('Error playing audio: $e'); } } else { print('Audio file not found for $translatedText'); } },

          my pubspec.yaml
           assets:
- images/
- audio/

loydvincent avatar Mar 24 '24 14:03 loydvincent

Same here, I've been successfully deploying onto a Chrome (Thorium) navigator all week. Today, by chance, I went for iOS Simulator as deployment and some assets json file that was loading perfectly, now it's not. The complain from flutter is the very same message than everyone else is having...

BTW, my assets section in pubspec.yaml has always been non defined (commented), and even so my program has been working perfectly for Chrome (Thorium) deploy.

Now, for iOS deploy, even I try to define the asset in pubspec.yaml, flutter still doesn't load my json file and finds it as non-existant/empty...

Any hint? (I'm going back to Chrome/Thorium deploy, no big deal, I'm just learning flutter and iOS deployment is not really needed as of now).

friguron avatar Apr 12 '24 08:04 friguron

I too had this issue where android was not showing images, meanwhile other platforms include web were showing images from the assets folder flawlessly, it turns out that if you append your path with "assets/" when you are referencing it, it would work just everywhere. for example:

Image(image: AssetImage("assets/images/icons/play.png"))

Similarly in pubspec.yaml add assets/ before all assets

  assets:
    - assets/images/icons/play.png

HarshalSharma avatar Jul 08 '24 14:07 HarshalSharma

I also faced that type of error in which I can run in web perfectly but in Android device it shows empty json file or not exist. The problem is in path when you have to run in Android you should provide all path to rootbundle.

Example:

Assets/files/lang.Json

Not like this files/lang.Json

Also try changing the name of json file . One time I solved the issue by changing the file name of json

Also give network permission in AndroidManifest.xml if you have foget. There is a code to do it without this network access will be denied.

shaikhfahad1678 avatar Aug 15 '24 11:08 shaikhfahad1678