To fix the unable to load asset in Flutter, you can either verify the image path that has correct image spelling or give a proper indentation in pubspec. yaml file. Here are the steps to fix the error: Step 1: Make sure you have added the correct image path (without a spelling mistake).
You should consider the indentation for assets
flutter:
assets:
- images/pizza1.png
- images/pizza0.png
More details:
flutter:
[2 whitespaces or 1 tab]assets:
[4 whitespaces or 2 tabs]- images/pizza1.png
[4 whitespaces or 2 tabs]- images/pizza0.png
I have the same issue. I've just run "$ flutter clean"
, then everything is OK.
More about this error
The simplest way is to reference your assest folder instead of the asset itself, just make sure you use proper indentations as the pubspec.yaml
is indent sensitive.
flutter:
uses-material-design: true
assets:
- images/
and you can simply access each image as
new Image.asset('images/pizza1.png',width:300,height:100)
For me,
flutter clean
,giving full patth in my image
image: AssetImage(
'./lib/graphics/logo2.png'
),
width: 200,
height: 200,
);
these three steps did the trick.
Encountered the same issue with a slightly different code. In my case, I was using a "assets" folder subdivided into sub-folders for assets (sprites, audio, UI).
My code was simply at first in pubspec.yaml- alternative would be to detail every single file.
flutter:
assets:
- assets
Indentation and flutter clean was not enough to fix it. The files in the sub-folders were not loading by flutter. It seems like flutter needs to be "taken by the hand" and not looking at sub-folders without explicitly asking it to look at them. This worked for me:
flutter:
assets:
- assets/sprites/
- assets/audio/
- assets/UI/
So I had to detail each folder and each sub-folder that contains assets (mp3, jpg, etc). Doing so made the app work and saved me tons of time as the only solution detailed above would require me to manually list 30+ assets while the code here is just a few lines and easier to maintain.
I also had this problem. I think there is a bug in the way Flutter caches images. My guess is that when you first attempted to load pizza0.png
, it wasn't available, and Flutter has cached this failed result. Then, even after adding the correct image, Flutter still assumes it isn't available.
This is all guess-work, based on the fact that I had the same problem, and calling this once on app start fixed the problem for me:
imageCache.clear();
This clears the image cache, meaning that Flutter will then attempt to load the images fresh rather than search the cache.
PS I've also found that you need to call this whenever you change any existing images, for the same reason - Flutter will load the old cached version. The alternative is to rename the image.
inside pubspec.yaml, DON'T USE TAB!
flutter:
<space><space>assets:
<space><space><space><space>assets/
example:
flutter:
assets:
assets/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With