Donath
Donath

Reputation: 31

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"

Upvotes: 3

Views: 2776

Answers (1)

ianhanniballake
ianhanniballake

Reputation: 199900

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.

Upvotes: 2

Related Questions