Unresolved reference: viewModelScope - Android KTX
Asked Answered
N

2

5

I'm trying to use the new viewModelScope() function provided by the new android ktx library.

In the gradle file, I've added:

implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.0.0'
implementation 'androidx.core:core-ktx:1.0.2'

but when I tied to access the viewModelScope(), I got Unresolved reference: viewModelScope error:

class MainViewModel(application: Application): AndroidViewModel(application) {

    fun fetchData(){
        viewModelScope.launch{

        }
    }
}

I don't understand what the problem is. Any idea? Thanks.

Never answered 28/5, 2019 at 17:10 Comment(1)
I am using lifecycle_version = "2.2.0". I am still getting the same error. Any solution for that?Persaud
E
6

Check the release notes https://developer.android.com/jetpack/androidx/releases/lifecycle#declaring_dependencies

viewModelScope is available from v2.1.x

So you need to bump your version in your gradle file.

Elute answered 28/5, 2019 at 17:28 Comment(2)
I am using lifecycle_version = "2.2.0", but the error is still persistingPersaud
@Persaud make sure you have import androidx.lifecycle.* in your source.Sealy
G
2

I have use in my project 2.1.0-beta01:

implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.1.0-beta01" 

This is less version then released on official site.
However, I use it because new version 2.2.0-alpha01 still has the Unresolved reference: viewModelScope - Android KTX error.

Gelatinate answered 26/6, 2019 at 11:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.