I’m trying to remember a custom object I don’t wan...
# compose
j
I’m trying to remember a custom object I don’t want to mutate during the composable lifecycle but I don’t want it to be recreated neither. It works well with
Copy code
val myCustomObject by remember { mutableStateOf(MyCustomObject()) }
If I try without
mutableStateOf
I get the following compiler error
Property delegate must have a 'getValue(Nothing?, KProperty0<*>)' method
. I tried to add this to
MyCustomObject
but it didn’t work
Copy code
operator fun getValue(thisRef: Nothing?, property: KProperty0<*>): MyCustomObject  = this
I get
'operator' modifier is not applicable to function: second parameter must be of type KProperty<*> or its supertype.
Is there a way to make this work? Or should I do it another way?
c
Get yourself familiar with property delegation, what the
by
keyword means and why you cannot use it without the
mutableState
https://kotlinlang.org/docs/delegated-properties.html The you will realize that you can just use
remember
and a simple assignment.
s
State has the
getValue
as an extension function. You just need to import it. If you’re using Android Studio or Fleet, that import should be suggested for you
If you’re trying to do it without state, you want
=
instead of
by
j
The main issue was that I tried to delegate the property of type
MyCustomObject
with
by
to the
MyCustomObject
class itself. From what read in the doc, that’s not how it’s meant to be used.
val myObject = remember { MyCustomObject() }
works fine, thanks for the help!
👍🏼 1