ViewModel: Unresolved reference

I want to implement ViewModel into my app, but I cant import viewModels() or activityViewModels(). Trying to follow tutorial but its still unresolved reference for me.

private val userViewModel: ProfileFlowFragment.UserViewModel by viewModels()

Imports:

implementation "androidx.fragment:fragment-ktx"
implementation "androidx.activity:activity-ktx"

// ViewModel
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.0"
// LiveData
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.3.0"

Answer

Did you specify the version in implementation "androidx.fragment:fragment-ktx" ? Anyway, I have these dependencies in my Gradle file, and everything is fine.

 implementation "androidx.core:core-ktx:$corektxVersion"
 implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_stdlibVersion"

Also add

apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

Leave a Reply

Your email address will not be published. Required fields are marked *