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

bmo

06/25/2020, 11:23 AM
Hello everyone, I made a very simple app using compose and it crashes with the error :
java.lang.IllegalArgumentException: Key 640122159 was already registered. Please call unregister before registering again
The code can be found here https://github.com/bmonjoie/ComposeTipsCalculator I have seriously no idea what I'm doing wrong so if someone could give me any pointers, that would be really appreciated
a

Andrey Kulikov

06/25/2020, 12:30 PM
hey, it is a known bug, where we don't do keys namespacing automatically yet, but it will be fixed. for now please change
(tips + null as TipsEntry?).forEach { entry ->
Entry(entry, onEntryChanged)
}
to
(tips + null as TipsEntry?).forEach { entry ->
key(entry) {
Entry(entry, onEntryChanged)
}
}
inside your Tips function
👍 1
b

bmo

06/25/2020, 1:06 PM
Thank you very much. I tried the following :
Copy code
(tips + null as TipsEntry?).forEach { entry ->
            val index = tips.indexOf(entry)
            key(if (index < 0) tips.size else index) {
                Entry(entry, onEntryChanged)
            }
        }
The first time I insert something in the empty field, it works. If i then proceed to the second line (now empty field) and type 1 number, the third line is added but if I enter again a number (in the second field, not changing the focus), it crashes with the same error
I think I figured out the problem, it's basically when I enter the same amount twice
So, because I'm using a data class, when I have the same value in two rows, even with the
key(index) {}
it crashes. If I remove the keyword
data
from my
TipsEntry
, it doesn't crash anymore
a

Andrey Kulikov

06/25/2020, 5:13 PM
right, basically you need to provide something unique in the key function. seems like some of your entries are equals to each other
👍 1