Flutter Default Image Placeholder

4,042

You don't need to wrap the condition posts[index].featuredImage.large in a NetworkImage widget.

Just check if the string coming from the API is null, then set the default Image as Image.asset('assets/images/placeholder.png') and if it is not null set the Image as NetworkImage(posts[index].featuredImage.large ).

Try the code below:

FadeInImage(
          placeholder: AssetImage('assets/images/placeholder.png'),
          image: posts[index].featuredImage.large  == null ?
          Image.asset('assets/images/placeholder.png') : NetworkImage(posts[index].featuredImage.large ),
          fit: BoxFit.cover,
        ),
Share:
4,042
Agent47
Author by

Agent47

Updated on December 20, 2022

Comments

  • Agent47
    Agent47 over 1 year

    In my data i have posts, but some having image urls and others have false value, while displaying i want to show the placeholder where value are false

    Here is my code :

         child: FadeInImage(
      placeholder: AssetImage('assets/images/placeholder.png'),
      image: NetworkImage(posts[index].featuredImage.large ) == null ? 
       Image.asset('assets/images/placeholder.png') : NetworkImage(posts[index].featuredImage.large ),
      fit: BoxFit.cover,
    ),
    
  • Agent47
    Agent47 almost 4 years
    featured_image":{"thumbnail":false,"medium":false,"large":fa‌​lse}}]
  • Constantin N.
    Constantin N. almost 4 years
    It looks like you didn't clearly explained your needs. as i see you want to look for each of those images if it is a link or not. if there is no link, then you will display a placeholder issn't i?