https://kotlinlang.org logo
#koin
Title
# koin
r

Romão

12/03/2023, 12:05 AM
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
c

Ciaran Sloan

12/03/2023, 6:25 PM
have you provided a definition for
SqlDelightQuestionDataSource
in a module somewhere? If not, thats your issue
r

Romão

12/03/2023, 11:50 PM
i think i did, Can't understand what i'm doing wrong. Could you please have a look in the following repo @Ciaran Sloan https://github.com/tmromao/MyQuizv4/tree/development-koin-ios many thanks
o

Osman Saral

12/04/2023, 8:42 AM
You might need to change the order of your modules in your initKoin function.
11 Views