why doesn't this compile: ```val countries by reme...
# compose
s
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
maybe try
mutableListOf
?
s
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
how about
mutableStateListOf
? haha I haven't quite got it either. The codelabs seem to cover
mutableStateOf
but not these other uses
z
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
so is the solution to use
mutableStateOf
even for lists?
z
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
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
It won’t, anything in the
remember
lambda will only be executed once