4

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
  • 69
  • 3
  • 13

2 Answers2

4

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,
        ),
void
  • 12,787
  • 3
  • 28
  • 42
0

This how you can do it

child: post[index].url==false? Image.assets('assets/images/placeholder.png'): FadeInImage(
fit: BoxFit.cover,
  placeholder: AssetImage('assets/images/placeholder.png'),
  image: posts[index].featuredImage.large == null ? 
   Image.asset('assets/images/placeholder.png') : NetworkImage(posts[index].featuredImage.large ),
),

But there will be an issue here if your url is declared as String since false is not a String and comparing different data type is not a good idea . Then you will have to check RuntimeType

E.g: post[index].url is String//true if url is a String

Constantin N.
  • 2,739
  • 2
  • 16
  • 27
  • featured_image":{"thumbnail":false,"medium":false,"large":false}}] – Agent47 May 20 '20 at 18:43
  • 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? – Constantin N. May 20 '20 at 18:51