Hello! Even though I use a very unique identifier,...
# compose
n
Hello! Even though I use a very unique identifier, I am sometimes getting below and app is crashing.
If you are using LazyColumn/Row please make sure you provide a unique key for each item.
Did anybody face a similar problem with
LazyList
?
s
We can't say much without seeing you code
n
Thanks. I basically get a list of instruments from VM and subscribe them as state
Copy code
val instruments by viewModel.output.instruments.subscribeAsState(initial = emptyList())
and passing this list to LazyList as follow
Copy code
LazyColumn(
    state = rememberLazyListState(),
    modifier = Modifier.fillMaxSize()
) {
    ...
    items(
        items = instruments,
        key = { it.isin },
        contentType = {
            when {
                it.derivativeTags?.optionTypeText != null -> CONTENT_TYPE_DERIVATIVES
                it.hasSavingsPlan -> CONTENT_TYPE_SAVINGS_PLAN
                sorting.criteria == Criteria.PositionSize -> CONTENT_TYPE_POSITION_SIZE
                else -> CONTENT_TYPE_METRIC
            }
        }
    ) { item ->
        ...
    }
}
each instrument has a unique isin
s
There are two possibilities: 1. You are passing another
item
which (mistakenly) has the same key of a `instrument`; 2.
isin
is not unique. Is it complex object?
n
I only perform sorting on the list as we have a sorter in the UI
Copy code
val instruments by viewModel.output.instruments.subscribeAsState(initial = emptyList())
this line might be suspicious? 🤔
isin
is an alphanumeric text like
US14583249X
c
Add some code to verify that all the
isin
are unique (just for debugging purposes)
Copy code
instruments.forEach { instrument ->
  require(instruments.none { it.isin == instrument.isin })
}
Can you confirm all the
isin
are unique?
n
Yes they are unique
XF000XRP0018
US88160R1014
US8676524064
US70450Y1038
US5949181045
XF000LTC0015
FR0000121014
JP3616200006
US30303M1027
XF000ETH0019
IE00BKM4GZ66
XF000LINK010
XF000BTC0017
XF000AVAX016
US0378331005
DE000TR04TT1
US02079K3059