I may be doing something wrong with LazyColumn or ...
# compose
t
I may be doing something wrong with LazyColumn or I've triggered a bug by using this code for transition animations:
Copy code
@Stable
private class AnimatedListModel<Key : Any>(
    val itemEnter: EnterTransition,
    val itemExit: ExitTransition
) {
    val hasTransitions =
        itemEnter != EnterTransition.None && itemExit != ExitTransition.None

    val itemVisibility = mutableStateMapOf<Key, MutableTransitionState<Boolean>>()

    @Composable
    fun LazyItemScope.AnimatedItem(item: ListItem<Key>, onClick: (() -> Unit)?) {
        if (hasTransitions) {
            val visible = itemVisibility.getOrPut(item.key) {
                MutableTransitionState(false).apply { targetState = true }
            }

            val itemScope = this

            AnimatedVisibility(visible, enter = itemEnter, exit = itemExit) {
                with (item) { itemScope.Content(onClick) }
            }
        } else {
            with (item) { Content(onClick) }
        }
    }
}
Copy code
2021-06-01 22:50:09.454 15735-15735/com.mercury E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.mercury, PID: 15735
    java.util.NoSuchElementException: Key LayoutNode@449837e children: 0 measurePolicy: androidx.compose.ui.node.LayoutNode$Companion$ErrorMeasurePolicy$1@8b77ab2 is missing in the map.
        at kotlin.collections.MapsKt__MapWithDefaultKt.getOrImplicitDefaultNullable(MapWithDefault.kt:24)
        at kotlin.collections.MapsKt__MapsKt.getValue(Maps.kt:344)
        at androidx.compose.ui.layout.SubcomposeLayoutState.takeNodeFromReusables(SubcomposeLayout.kt:297)
        at androidx.compose.ui.layout.SubcomposeLayoutState.precompose(SubcomposeLayout.kt:380)
        at androidx.compose.foundation.lazy.LazyListPrefetcher.subcompose(LazyListPrefetcher.android.kt:198)
        at androidx.compose.foundation.lazy.LazyListPrefetcher.run(LazyListPrefetcher.android.kt:170)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at android.app.ActivityThread.main(ActivityThread.java:7727)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)
Ah, updating
itemVisibility
inside a
LaunchedEffect
(to clear out existing state) causes this. Updating a
var model = remeber { AnimatedListModel(...) }
in a
LaunchedEffect
works fine.