Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image cannot be null using FadeInImage and url [Flutter]

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.

enter image description here

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: enter image description here

like image 470
Vedo Avatar asked Oct 16 '25 21:10

Vedo


1 Answers

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(...),
like image 188
John Joe Avatar answered Oct 19 '25 13:10

John Joe