FlutterError: Unable to load asset
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 assets 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
Image.asset('images/pizza1.png', width:300, height:100)
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
After all this, you can make a hot-restart.
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.