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
Vadzim Filipovich
08/10/2021, 1:16 PM
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
Colton Idle
08/10/2021, 3:19 PM
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
Vadzim Filipovich
08/10/2021, 3:43 PM
@Colton Idle notice, that state could be error and it will fall back to else
c
Colton Idle
08/10/2021, 3:49 PM
Use
Copy code
ImagePainter.State.Error
instead?
v
Vadzim Filipovich
08/10/2021, 3:57 PM
Yep
m
MaxUt
08/10/2021, 3:57 PM
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()
}
}