I have a dumb question: ```val tokens = viewModel....
# compose
y
I have a dumb question:
Copy code
val tokens = viewModel.tokens.observeAsState(listOf())
I use mock viewModel for preview with hardcoded livedata
Copy code
override val stables: LiveData<List<StableTokenWithMetaAndRate>> = liveData {
  listOf(StableTokenWithMetaAndRate.USDp, StableTokenWithMetaAndRate.EURp)
}
However in the preview show me an empty list (((
z
I believe that
listOf()
you’re passing to
observeAsState
is the initial value, so that’s why it’s showing it in the preview. The non-interactive preview won’t run any coroutines, so
observeAsState
never actually starts observing your
LiveData
. At least I’m guessing that’s what it is.
y
Oh. So I need to put my sample date as initial values
z
Better yet, just use the overload that uses the `LiveData`’s current value as the the initial value and doesn’t take any parameters: https://developer.android.com/reference/kotlin/androidx/compose/runtime/livedata/package-summary
(although i’m not sure why that returns a
State<T?>
– might be a bug, it should just be
State<T>
i’d think)
d
I think you need to call
emit
in your
liveData { .... }
.
a
@Zach Klippenstein (he/him) [MOD] `LiveData`'s Kotlin interoperability is poor and its value is always nullable, which means it is possible to emit
null
from
LiveData<T>
.
z
Oh, that makes sense 😣