I am facing a weird problem when trying to load an image from a URL, when I try the following :
<code> val painter1 = rememberAsyncImagePainter(
model = targetUrl,
onSuccess = {
Log.i("MYTAG", "BasicContentCard: Success!")
}
)
Log.i("MYTAG", "BasicContentCard: ${painter1.state}")
Image(
painter = painter1,
contentDescription = null,
modifier = Modifier.fillMaxSize()
)
</code>
<code> val painter1 = rememberAsyncImagePainter(
model = targetUrl,
onSuccess = {
Log.i("MYTAG", "BasicContentCard: Success!")
}
)
Log.i("MYTAG", "BasicContentCard: ${painter1.state}")
Image(
painter = painter1,
contentDescription = null,
modifier = Modifier.fillMaxSize()
)
</code>
val painter1 = rememberAsyncImagePainter(
model = targetUrl,
onSuccess = {
Log.i("MYTAG", "BasicContentCard: Success!")
}
)
Log.i("MYTAG", "BasicContentCard: ${painter1.state}")
Image(
painter = painter1,
contentDescription = null,
modifier = Modifier.fillMaxSize()
)
The images load correctly and I even get the success logs for the painter state.
The problem emerges when I try to put the same code behind some condition (So that I can show some placeholder composable until the image is fully loaded):
<code> if (painter1.state is AsyncImagePainter.State.Success){
Image(
painter = painter1,
contentDescription = null,
modifier = Modifier.fillMaxSize()
)
} else {
PlaceHolderComposable()
}
</code>
<code> if (painter1.state is AsyncImagePainter.State.Success){
Image(
painter = painter1,
contentDescription = null,
modifier = Modifier.fillMaxSize()
)
} else {
PlaceHolderComposable()
}
</code>
if (painter1.state is AsyncImagePainter.State.Success){
Image(
painter = painter1,
contentDescription = null,
modifier = Modifier.fillMaxSize()
)
} else {
PlaceHolderComposable()
}
The image never loads and even the debug logs indicate that the state of the painter is Loading :
<code>MYTAG com.rahul.samplelauncher BasicContentCard: Loading(painter=null)
</code>
<code>MYTAG com.rahul.samplelauncher BasicContentCard: Loading(painter=null)
</code>
MYTAG com.rahul.samplelauncher BasicContentCard: Loading(painter=null)
How do I get around this problem, I want to show a different composable while the image loads.