Is it possible to conditionally load an asset image, only if it exists? Therefore you could load a default image if it does not exist in the Assets bundle.
Asked
Active
Viewed 3,261 times
2
-
Check this [answer](https://stackoverflow.com/questions/52908923/check-if-asset-exist-in-flutter) – Abdelbaki Boukerche Apr 08 '20 at 21:18
-
Thanks, is there a way to do this without relying on catching an exception? – MonkeyBrain Apr 08 '20 at 21:24
1 Answers
2
One way you can check if the asset image exists is by trying to display it. If it throws an Exception, display a different widget instead.
Future<Widget> getAssetImage(String path) async {
try {
await rootBundle.load(path);
return Image.asset(path);
} catch (_) {
return SizedBox(); // Return this widget
}
}
Otherwise, you can check the AssetManifest.json and cross-check the asset to be used with the list before trying to access it.

Omatt
- 8,564
- 2
- 42
- 144