2

I am just playing with compose multiplatform and injecting viewModel using Koin. Here are my koin Initialization in common Module:

fun initKoin(config: KoinAppDeclaration? = null) {
    startKoin {
        config?.invoke(this)
        modules(
            sharedModule,
            platformModule
        )
    }
}

expect val platformModule: Module

val sharedModule = module {
    singleOf(::DataRepositoryImpl).bind<DataRepository>()
    viewModelOf(::HomeViewModel)
}

My actual implementation are like this in specific platform:

actual val platformModule = module {
    single { createHttpClient(OkHttp.create()) }
}

My app runs fine in Android platform. But when I try to run it in desktop it doesnt start and gives an error dialog saying that "Can not create instance of Viewmodel..". So far I found out that it only happens when I am using any coroutinescope in the viewmodel. If I just use a plain viewmodel the app runs perfectly.

Please help!

1 Answer 1

1

I have finally fixed my issue, I needed to add the swing library to make Dispatchers.main() work in desktop variant. I just added this:

implementation(libs.kotlinx.coroutines.swing)

and my version catalog file I have added

kotlinx-coroutines-swing = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-swing", version.ref = "kotlinxCoroutinesSwing" }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.