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

Sam

11/12/2020, 11:01 PM
why doesn't this compile:
Copy code
val countries by remember { listOf(Locale.getISOCountries().map { Locale("", it) }) }
with exception:
Type 'TypeVariable(T)' has no method 'getValue(Nothing?, KProperty<*>)' and thus it cannot serve as a delegate
i

Ian Sikes

11/12/2020, 11:02 PM
maybe try
mutableListOf
?
s

Sam

11/12/2020, 11:03 PM
does not work either @Ian Sikes
i suspect i have a misunderstanding with how to store lists in state with
remember
but i can't find any examples
i

Ian Sikes

11/12/2020, 11:07 PM
how about
mutableStateListOf
? haha I haven't quite got it either. The codelabs seem to cover
mutableStateOf
but not these other uses
z

Zach Klippenstein (he/him) [MOD]

11/13/2020, 12:28 AM
mutableStateOf
returns a
MutableState
, which has
getValue
(and
setValue
) defined as extension functions, which is why that works.
remember
just returns whatever you return from the lambda – it doesn’t change the type. So if what you’re returning can’t act as a delegate, this won’t work.
s

Sam

11/13/2020, 12:29 AM
so is the solution to use
mutableStateOf
even for lists?
z

Zach Klippenstein (he/him) [MOD]

11/13/2020, 12:29 AM
Neither
List
nor
SnapshotStateList
can be delegates
so is the solution to use mutableStateOf even for lists?
Yes, the list is your state here.
Although you only need to do that if you actually need to change it. If you don’t, just remembering the list directly is fine, but then use
=
instead of
by
.
s

Sam

11/13/2020, 12:31 AM
that's what i was about to ask, since the list here is immutable.. just want to make sure it doesn't recalculate the map transform on every render
z

Zach Klippenstein (he/him) [MOD]

11/13/2020, 12:32 AM
It won’t, anything in the
remember
lambda will only be executed once
2 Views