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,
),
Author by
Agent47
Updated on December 20, 2022Comments
-
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 arefalse
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 almost 4 yearsfeatured_image":{"thumbnail":false,"medium":false,"large":false}}]
-
Constantin N. almost 4 yearsIt 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?