Jetpack Compose - Unresolved reference: observeAsState
Asked Answered
C

5

89

I'm learning Jetpack Compose and I was trying to make a View Model for my @Composable.

In documentation (https://developer.android.com/codelabs/jetpack-compose-state#3) for observing state changes in composable they use observeAsState but in my implementation, the method cannot be found. I get instead Unresolved reference: observeAsState

ViewModel

class MainActivityViewModel : ViewModel() {
    val list: LiveData<MutableList<String>> = MutableLiveData(mutableListOf("Ana", "are", "mere"))

    fun addString(item: String) {
        val list: MutableList<String> = list.value!!
        list.add(item)
    }

}

Composable enter image description here

I am using Compose 1.0.0-beta01

Chook answered 10/3, 2021 at 7:48 Comment(1)
Just FYI if anyone reads this and doesn't follow: the original question from 2021 refers to the State Codelab v2021. In 2022 a new State Codelab was created and uploaded, so the url above doesn't take you to a place where you can see the code and this API anymore, but a page in the new codelab. Cheers!Notability
T
185

observeAsState is part of the runtime-livedata library.

Add the dependency to your module's build.gradle file. Replace $compose_version with the version of compose which you use, e.g. 1.0.0-beta01:

implementation "androidx.compose.runtime:runtime-livedata:$compose_version"

You can find the available versions here in Google's Maven repository.

Turnage answered 10/3, 2021 at 8:10 Comment(2)
Still, the same documentation is not updated for required dependenciesDolhenty
Is it part of Compose?Optics
P
14

Not exactly answering your question, but as a suggestion you could migrate to Flow instead of using live data.

ViewModel ->

val yourList: MutableStateFlow<List<String>> = MutableStateFlow(listOf("String1","String2","String3"))

Composable ->

val yourList by yourViewModel.yourList.collectAsState()
Purvis answered 3/7, 2022 at 7:18 Comment(4)
The same.. The method is not foundBestraddle
did you properly imported the setters and getters?Purvis
the issue was with different libraries and versionsBestraddle
my question is which is convient way to use state like above observestate with live data ot collectAsstate with flow, I'm confused to which one useObsolescent
D
3

CollectAsStateWithLifecycle is now recommended for Android by google devs and it's lifecycle api is also stable.Flow is also recommended by devs.

Differentia answered 8/7, 2023 at 16:39 Comment(0)
T
0

Face the same issue with compose-bom. i.e androidx.compose:compose-bom:2024.05.00

Removing compose-bom and adding separate implementation of androidx.compose.runtime:runtime-livedata:$compose_version resolved my issue

Tunisia answered 13/5 at 16:36 Comment(0)
M
0

i think this is a response from api right? if it is in this case i think you should use collectAsState(). and if you want the list to survive any recomposition you can use collectAsStateWithLifeCycle(). And then when your response comes it will be collected in the list and you can use it ex. list[].employee.

Middelburg answered 24/6 at 5:33 Comment(1)
This question already has an accepted answer, your answer does not provide any further information.Pleasance

© 2022 - 2024 — McMap. All rights reserved.