Image cannot be null using FadeInImage and url [Flutter]

195

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(...),
Share:
195
Don
Author by

Don

Junior Software Engineer

Updated on January 03, 2023

Comments

  • Don
    Don over 1 year

    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

  • Don
    Don about 2 years
    First of all thank you for quick reply I followed your instructions and this is what my code is now along with the error (updated question)
  • John Joe
    John Joe about 2 years
    question not updated.
  • Don
    Don about 2 years
    Check again I updated it few second ago
  • John Joe
    John Joe about 2 years
    "A non-null String must be provided to a Text widget" The latest error shows the String in the Text widget is null. Did the error point to which line?
  • Don
    Don about 2 years
    When I check the text.dart the line 378 its this : assert( data != null, 'A non-null String must be provided to a Text widget.', ),
  • John Joe
    John Joe about 2 years
    Post the full exception.
  • Don
    Don about 2 years
    I edited question with full exception
  • John Joe
    John Joe about 2 years