Skip to content

Conversation

@hehua2008
Copy link
Contributor

@hehua2008 hehua2008 commented Jan 3, 2024

val image: T? by produceState<T?>()

should use "load" as a key, or else it won't be re-launched for the new "load".

-     val image: T? by produceState<T?>(null) {
+     val image: T? by produceState<T?>(null, load) {
         value = withContext(Dispatchers.IO) {
             try {
                 load()
             } catch (e: IOException) {
                 // instead of printing to console, you can also write this to log,
                 // or show some error placeholder
                 e.printStackTrace()
                 null
             }
         }
     }

And

painterFor = { remember { BitmapPainter(it) } }

should use "it" as a key for the "remember", or else the "painterFor" won't update for the new "image" loaded by "produceState".

         AsyncImage(
             load = { loadImageBitmap(File("sample.png")) },
-            painterFor = { remember { BitmapPainter(it) } },
+            painterFor = { remember(it) { BitmapPainter(it) } },
             contentDescription = "Sample",
             modifier = Modifier.width(200.dp)
         )

"val image: T? by produceState<T?>()" should use "load" as a key, or else it won't be re-launched for the new "load".
And "painterFor = { remember { BitmapPainter(it) } }" should use "it" as a key for the "remember", or else the "painterFor" won't update for the new "image" loaded by "produceState".
@hehua2008 hehua2008 changed the title Fix the bug that AsyncImage demo cannot update new images Fix the bug that AsyncImage demo cannot update new images in tutorials/Image_And_Icons_Manipulations/README.md Apr 1, 2024
@hehua2008
Copy link
Contributor Author

Please fix this bug to avoid confusing tutorials learners!

@MatkovIvan MatkovIvan requested a review from igordmn April 8, 2024 08:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant