https://kotlinlang.org logo
#compose
Title
# compose
r

Ruben Quadros

11/05/2021, 2:04 PM
Hi guys, I keep getting
java.lang.IllegalArgumentException: Key was already used. If you are using LazyColumn/Row please make sure you provide a unique key for each item
. I am sure all the keys are unique and I even logged them to be sure. Also this happens when items are rapidly added to lazy column. Please find the sample code in the thread
🧵 1
Copy code
@Composable
fun UiComponent() {
    LazyColumn(
                verticalArrangement = Arrangement.spacedBy(12.dp),
                state = scrollState,
                reverseLayout = true
            ) {
                items(
                    items = viewmodel.messages,
                    key = { item -> item.hashcode() },
                    itemContent = { item: Entity ->
                        if (item.isDeleted) {
                            //show deleted ui
                        } else {
                            //show messages
                        }
                    })
            }
        }
}

VM {

    init { 
       observeDataFromDB()
    }

    private val _messages: MutableList<Entity> = mutableStateListOf()
    val messages: List<Entity> = _messages


    fun observeDataFromDB() {
        viewModelScope.launch {
            repo.getData().collect {
                _messages.apply {
                    addNewItem(it)
                }
            }
        }
    }

}


//extensions
fun MutableList<Entity>.addNewItem(entity: Entity) {
    if (this.size >= MAX_SIZE) {
        removeLast()
    }
    Log("existing ${this.toList().map { "${it.hashCode()}" }}")
    Log("adding new ${entity.hashCode()}")
    this.add(0, entity)
}

//id is primary key
data class Entity(val id: String, val isDeleted: Boolean, val message: String)
Any help would be great! Have been stuck with this for sometime now and not sure how to proceed 😭 Have tried using 
id
 as the key and I still get the same error. (edited)
c

Csaba Kozák

11/05/2021, 2:53 PM
There must be duplicated items in your list.
r

Ruben Quadros

11/05/2021, 3:26 PM
I don't see any duplicates in the logs 😅. Also the key I get in the stacktrace is different than the one I am adding. java.lang.IllegalArgumentException: Key
3c5bb4e5-b593-49ec-b2a2-dcfbe2793410
was already used. If you are using LazyColumn/Row please make sure you provide a unique key for each item Here are the debug logs that I printed
Copy code
//printed from extension before adding new item
existing [
    ea7c5564-46be-480b-afcb-58fff39546d8,
    36ed3e44-aea7-4e50-8df1-96af7d75abaf,
    3c5bb4e5-b593-49ec-b2a2-dcfbe2793410,
    41c7a7d2-c2d7-40c6-a894-bc17c2bc21da,
    09b46d7d-8c17-4662-bff6-d73bcd9e240d,
    db6b3e68-6b1e-4e72-a003-55a8bc136e24,
    3bfb9cb6-0b7b-4f8d-9b58-95aa341c25d2,
    55bdcb3a-efab-41b4-9192-3fa9bfbd9a5d,
    393f7050-3ab0-475c-b0ed-ea3f0aa7eb55,
    77549c09-37f6-4701-ad10-36b09090db13,
    38975fff-91be-4edd-904f-97f9a9d97909,
    b76bfa15-5ab1-44af-a807-c3ab60e0c422,
    2d4d577f-d8bb-4d1a-811a-da24d0316bcc,
    c58e1b39-2025-4df3-8acf-2675b910fb3f,
    b06815f1-8ad6-4fed-8a3d-ef542d328e4b,
    90635fdf-f388-4a97-ba67-58dce3add477,
    a1262747-084a-41bc-995e-467734e5afb6,
    34e5b3e1-9d1a-4128-b1a0-c89c6abc7bfc,
    01575cdf-9427-4038-b127-5d5414f7ee79
]

//printed from extension the new item to be added
adding new 0714f298-63b5-4310-93d6-540e2dcc0be3
j

John Nichol

11/05/2021, 3:26 PM
From a quick read of the code you seem to be using item.hashcode() to generate the key - but as hashcode() isn't unique this isn't a safe strategy
r

Ruben Quadros

11/05/2021, 3:27 PM
Yes, I have used
id
which is guaranteed to be unique but I still get the same error
j

John Nichol

11/05/2021, 3:29 PM
ok - but the key that it's complaining about is in both your before and after lists you just pasted - so I assume somehow you are adding an entry again
r

Ruben Quadros

11/05/2021, 3:32 PM
Sorry,
existing
and
in ui
is the same list but printed from 2 different places.
existing
- from extension function and
in ui
- from composable. I realise this could be confusing will remove the one which is printed from composable
9 Views