dnfield / flutter_svg

SVG parsing, rendering, and widget library for Flutter
MIT License
1.66k stars 453 forks source link

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

Open KiraResari opened 1 year ago

KiraResari commented 1 year ago

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?

hansheng654 commented 1 year ago

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

H1eu69 commented 1 year ago

Same here. Have you found any solutions?

DinoDafor commented 11 months ago

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

MohammadRezaRaad commented 8 months ago

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

assets:

you MUST write full name of your svg file

if you do this, you will have errore:

assets:

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

Dolapo-A commented 7 months ago

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

Ali-Fadaei commented 7 months ago

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

Dolapo-A commented 7 months ago

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 commented 7 months ago

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

what does your path look like in pubspec.yaml

Ali-Fadaei commented 7 months ago

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

what does your path look like in pubspec.yaml

assets:

cristiancamilo070 commented 6 months ago

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
      ),
loydvincent commented 5 months ago

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/
friguron commented 4 months ago

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).

HarshalSharma commented 2 months ago

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
shaikhfahad1678 commented 3 weeks ago

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.