https://kotlinlang.org logo
Title
s

Saurav Suman

02/17/2023, 10:48 AM
Hello, Can someone please explain the problem of using key in lazy column? I’m facing the same problem.
<https://stackoverflow.com/questions/74381205/removing-item-from-the-list-causes-wrong-display-in-lazycolumn>
a

abbic

02/17/2023, 11:33 AM
as the reply to the question explains, you need the items inside the
items
callback to be associated with a key. that way, when an item is removed, the LazyColumn knows what is removed. In that example, say each
Person
had a field called id, which was unique to it. Then you'd write
items(people.size, key = { index -> people[index].id }) { etc ...
s

Saurav Suman

02/17/2023, 11:34 AM
Someone told me there is a problem or crash while using key in lazy column, is it so?
a

abbic

02/17/2023, 11:34 AM
this
key
function associates an index with an id, so when an item is removed and all the indices of the items change, the key callback now reconnects the index to the item it should contain properly
dont see why, have you tried it?
s

Saurav Suman

02/17/2023, 11:35 AM
not yet
a

abbic

02/17/2023, 11:36 AM
give it a go, if it crashes you then actually have an exception to google
s

Saurav Suman

02/17/2023, 11:36 AM
good one 👍
Thanks abbic