Passing a LazyListState as a variable into a Lazy Column is creating a weird bug I had never encountered.
val listState = rememberLazyListState()
LaunchedEffect(key1 = state.messages.size) {
if (state.messages.isNotEmpty() && listState.firstVisibleItemIndex < 5) listState.animateScrollToItem(0)
}
Here, the Log Statement (printed out on every recomposition) gives the latest state of the first "msg" after every change that is made to that message.
But when the callback "onReply()" is invoked, the argument "msg" still has the old state.
How on earth could that happen when UI got updated but callback is fired with an old state?
LazyColumn(
modifier = Modifier.fillMaxSize(),
reverseLayout = true,
state = listState
) {
itemsIndexed(
items = state.messages,
key = { _, m -> m.msgId }
) { i, msg ->
if (i == 0) {
Log.i("ChatScreen", "Message with ID: ${msg.msgId} is ${msg.state.name}")
}
MessageItem(
message = msg,
onReply = {
viewModel.reply(msg)
}
)
}
}
When variable listState was removed from LazyColumn or rememberLazyListState() is passed into, the bug goes away.
How can I solve the issue?