skip to Main Content

When I use to call an image by API it shows it need a null Checker

DownloadsImageWidget(
                    imageList:
                        '$imageAppendUrl${state.downloads?[1].posterPath}',
                    margin: const EdgeInsets.only(),
                    size: Size(
                      size.width * 0.4,
                      size.height * 0.30,
                    ),
                  ),

I tried to put a null checker ? , then I will never get a image because of wrong URL.
its shows this error HTTP request failed, statusCode: 404, https://image.tmdb.org/t/p/w500null

when I tried to give ! Null check operator used on a null value I get this error.

2

Answers


  1. I guess you can try to add a ternary operator to check whether your state.downloads is not null:

    DownloadsImageWidget(
      imageList: state.downloads == null ? null : '$imageAppendUrl${state.downloads[1]?.posterPath}',
      margin: const EdgeInsets.only(),
      size: Size(
        size.width * 0.4,
        size.height * 0.30,
      ),
    ),
    
    Login or Signup to reply.
  2. I assume that the posterPath is the non-null attribute.

    Then you can check if the downloads is empty or not before passing its list item.

    Example:

    if(state.downloads.isNotEmpty)
      DownloadsImageWidget(
        imageList: '$imageAppendUrl${state.downloads?[1].posterPath}',
        margin: const EdgeInsets.only(),
        size: Size(size.width * 0.4,size.height * 0.30),
      ),
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search