Valentin Stamate
Valentin Stamate

Reputation: 1439

Jetpack Compose - Unresolved reference: observeAsState

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

Upvotes: 93

Views: 40194

Answers (7)

Muhammad Umair Zahid
Muhammad Umair Zahid

Reputation: 1881

You can use the dependency with the latest version

implementation("androidx.compose.runtime:runtime-livedata:1.6.0")

However, it is recommended to use flows with collectAsState() with compose since Flow is more modern, lightweight, and fits better into Kotlin coroutines:

 dependencies {
    implementation("androidx.compose.runtime:runtime:1.6.0")
    implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.2") 
}

Upvotes: 1

SMBiggs
SMBiggs

Reputation: 11726

Hoffman's answer here is indeed the right one. But of course it is three years old and out of date. As of 2024-08, this is the line to add to your build.gradle.kts (app):

implementation(libs.androidx.runtime.livedata)

Upvotes: 0

Priyanshu Raj
Priyanshu Raj

Reputation: 33

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.

Upvotes: 0

Zulqarnain
Zulqarnain

Reputation: 651

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

Upvotes: 0

Premjit
Premjit

Reputation: 67

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

Upvotes: 3

mario chois
mario chois

Reputation: 206

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()

Upvotes: 15

Alexander Hoffmann
Alexander Hoffmann

Reputation: 6049

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.

Upvotes: 193

Related Questions