11

I am trying to use Koin to inject my viewModel (which has some dependencies as well) like this:

Unresolved Reference

I don't understand why it cannot find getViewModel when I have the following import:

Koin import

I am using this Koin version: implementation "io.insert-koin:koin-android:$koin_version"

where $koin_version = '3.2.0-beta-1'

Any thoughts why my import is ignored here?

FailedUnitTest
  • 1,637
  • 3
  • 20
  • 43
  • Check out [Why should I not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/a/285557/3585796) and replace images with [formatted](https://stackoverflow.com/help/formatting) code – Phil Dukhov Mar 29 '22 at 15:57

2 Answers2

16

You're using a wrong import, you should use:

import org.koin.androidx.compose.getViewModel

To use it you need the following dependency:

implementation("io.insert-koin:koin-androidx-compose:$koinVersion")
Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220
3

Here's how I did it in koin 3.3.2

@Composable
fun HomeScreen(viewModel: PokemonViewModel = koinViewModel()) {
}

I also added koin-core to build.gradle(:app)

def koin_version = '3.3.2'
implementation "io.insert-koin:koin-core:$koin_version"
implementation "io.insert-koin:koin-android:$koin_version"
implementation 'io.insert-koin:koin-androidx-compose:3.4.1'

SOURCE

francis
  • 3,852
  • 1
  • 28
  • 30