Hello, please help I'm trying to use Koin in a KMM...
# multiplatform
r
Hello, please help I'm trying to use Koin in a KMM project with Voyager lib My Screen
Copy code
@OptIn(ExperimentalVoyagerApi::class)
data object QuestionScreen : Screen {

    override val key : ScreenKey
        get() = KEY_QUESTION_SCREEN

    @Composable
    override fun Content() {
        val navigator = LocalNavigator.currentOrThrow

        val screenModel = navigator.getNavigatorScreenModel<QuestionScreenModel>()
        //var screenModel = getScreenModel<QuestionScreenModel>()

        //objects are a list of questions
        //val objects by screenModel.getObject().collectAsState()

        val state by screenModel.state.collectAsState()
My Koin
Copy code
val screenModelsModule = module {
    factoryOf(::ListScreenModel)
    factoryOf(::DetailScreenModel)
    factoryOf(::QuestionScreenModel)
}
ViewModel
Copy code
class QuestionScreenModel(
    questionDataSource: SqlDelightQuestionDataSource,
) : ScreenModel {
getting the following
Copy code
FATAL EXCEPTION: main
                                                                                                    Process: com.jetbrains.kmpapp, PID: 7162
                                                                                                    java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
                                                                                                    	at
                                                                                                    com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:502)
                                                                                                    	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
                                                                                                    Caused by: java.lang.reflect.InvocationTargetException
                                                                                                    	at java.lang.reflect.Method.invoke(Native Method)
                                                                                                    	at
                                                                                                    com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
                                                                                                    	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 
                                                                                                    Caused by: org.koin.core.error.InstanceCreationException: Could not create
                                                                                                    instance for
                                                                                                    '[Factory:'com.jetbrains.kmpapp.screens.components.QuestionScreenModel']'
                                                                                                    	at org.koin.core.instance.InstanceFactory.create(InstanceFactory.kt:57)
                                                                                                    	at
                                                                                                    org.koin.core.instance.FactoryInstanceFactory.get(FactoryInstanceFactory.kt:38)
                                                                                                    	at
                                                                                                    org.koin.core.registry.InstanceRegistry.resolveInstance$koin_core(InstanceRegistry.kt:116)
                                                                                                    	at org.koin.core.scope.Scope.resolveValue(Scope.kt:247)
                                                                                                    	at org.koin.core.scope.Scope.resolveInstance(Scope.kt:233)
                                                                                                    	at org.koin.core.scope.Scope.get(Scope.kt:212)
                                                                                                    	at
                                                                                                    com.jetbrains.kmpapp.screens.components.QuestionScreen.Content(QuestionScreen.kt:113)
                                                                                                    	at
                                                                                                    cafe.adriel.voyager.navigator.NavigatorKt$CurrentScreen$1.invoke(Navigator.kt:48)
                                                                                                    	at
                                                                                                    cafe.adriel.voyager.navigator.NavigatorKt$CurrentScreen$1.invoke(Navigator.kt:47)
                                                                                                    	at
                                                                                                    androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:108)
                                                                                                    	at
Any clue about what iam doing wrong? thanks
🧵 8
d
I hit a similar situation yesterday on a project I'm working on. For me, the problem was that I wasn't calling initKoin() I wasn't passing in a reference to my project's class that extends Application. I needed to update the class to look like this:
Copy code
class VariantApp : Application() {
    override fun onCreate() {
        super.onCreate()

        initKoin(
            appModule = module {
                single<Context> { this@VariantApp }
            },
            viewModelsModule = module {
                viewModel {
                    ServerViewModel(get())
                }
            }
        )
    }
}
The difference was that I hadn't included the appModule reference, so Koin couldn't find the Context object it needed to do its work.
r
I have added it, thanks for the hint @Darryl Pierce. Now I'm hitting again in Koin Could you please have a look in the following repo https://github.com/tmromao/MyQuizv4/tree/development-koin-ios Many thanks in advance
d
I’m not terribly familiar with Kotlin, only having just started my first project recently. I just saw similarities between your error and mine.
r
@Darryl Pierce ok thanks for the hint. It help me. I'm now facing pain with Koin. Cant understand what im doing wrong, seems everything is correct, but keep getting an error. i'm far from knowledge, experience.
👍 1