Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't use liveData or viewModelScope.launch

I'm trying to use these two builders for coroutines in my app, but in my ViewModel I can't import them or they do not pop up.

These are my dependencies:

   implementation "androidx.lifecycle:lifecycle-extensions:2.2.0-rc02"

   implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.2'

and in my ViewModel

class MainViewModel(): ViewModel() {

init{

  viewModelScope ----> does not work , marked in red
  val data = liveData {} ----> does not work, marked in red
}


}

I rebuilt, cleaned, and restarted with invalidating cache, but I can't use them

like image 269
SNM Avatar asked Dec 02 '19 19:12

SNM


1 Answers

Add the ViewModel ktx lib:

implementation "androidx.lifecycle:lifecycle-viewmodel-ktx$lifecycle_version"

Available after AndroidX lifecycle v2.1.0

like image 101
Bruno Martins Avatar answered Sep 27 '22 21:09

Bruno Martins