Hello so I added a way to display my url images in a list but because Im fetching this information from public API some of them are missing URLs and errors occur, I tried using placeholder and image errorbuilder but that didnt help
Here is my code:
child: FadeInImage.assetNetwork(
height: 100,
width: 100,
fit: BoxFit.fill,
image: newsList[index].urlToImage,
placeholder: 'images/placeholder.png',
imageErrorBuilder: (context, error, StackTrace) {
return const Image(
height: 100,
width: 100,
image: AssetImage("images/placeholder.png"));
},
),
and Error:
══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following assertion was thrown building:
'package:flutter/src/widgets/fade_in_image.dart': Failed assertion: line 234 pos 15: 'image !=
null': is not true.
Updated Code:
child: newsList[index].urlToImage == null
? Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("images/placeholder.png"),
),
),
)
: FadeInImage.assetNetwork(
height: 100,
width: 100,
fit: BoxFit.fill,
image: newsList[index].urlToImage,
placeholder: 'images/placeholder.png',
imageErrorBuilder:
(context, error, StackTrace) {
return const Image(
height: 100,
width: 100,
image:
AssetImage("images/placeholder.png"));
},
),
and error message:
imageErrorBuilder will only be called if there is an error occurs during image loading, example the image path does not exist.
To fix your issue, you have to check whether url is null or empty. If it is null, display another widget, else display FadeInImage
.
newsList[index].urlToImage == null ? DisplayNoImage: FadeInImage.assetNetwork(...),
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