Is there a way to hide an image that couldn't be l...
# compose
m
Is there a way to hide an image that couldn't be loaded with Coil ? I've tried using the onExecute function but the states always seems to be EMPTY. Note that I have set a height to my Image composable which is why I want to hide it if it fails.
v
You could render an image if state flag is true, so you could create a custom image composable, inside this composable you have state //isError//, and if isError false - display image, if not - display nothing. On coil error callback you could set isError to true
c
Copy code
val painter = rememberImagePainter("http")
val state = painter.state
if (state is ImagePainter.State.Loading) {
    
} else {
    Image(
        painter = painter,
        modifier = Modifier.size(48.dp)
    )
}
does this help?
v
@Colton Idle notice, that state could be error and it will fall back to else
c
Use
Copy code
ImagePainter.State.Error
instead?
v
Yep
m
Thank you guys 🙂 I ended up using
Copy code
@Composable
fun ImageWrapper(painter: ImagePainter, content: @Composable () -> Unit) {
    if (painter.state is ImagePainter.State.Success || painter.state is ImagePainter.State.Empty) {
        content()
    }
}