Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

viewModelScope unresolved reference

I was trying to: import androidx.lifecycle.viewModelScope and I get unresolved reference. I tried old StackOverflow links mentioning that I had to change implementation's version to 2.2.0 or higher or insert runtime dependence but I already have done this, as it is shown below.

These are my dependencies and plugins

    // Navigation
    implementation 'androidx.navigation:navigation-fragment-ktx:2.2.2'
    implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'

    // Room
    implementation "androidx.room:room-runtime:2.3.0"
    kapt "androidx.room:room-compiler:2.3.0"
    implementation "androidx.room:room-ktx:2.3.0"
    androidTestImplementation "androidx.room:room-testing:2.3.0"

    // Lifecycle


    implementation "androidx.lifecycle:lifecycle-common-java8:2.3.1"
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1"
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.3.1"
    implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.3.1"


    // Kotlin
    api "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2"
    api "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.2"
like image 421
Donath Avatar asked Dec 21 '25 20:12

Donath


1 Answers

As per the viewModelScope documentation, viewModelScope is an extension property on the ViewModel class.

Therefore to use viewModelScope, make sure you are within a class that extends ViewModel.

like image 199
ianhanniballake Avatar answered Dec 23 '25 11:12

ianhanniballake



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!