Could not create instance of ViewModel using Koin

PressRex profile image
by PressRex
Could not create instance of ViewModel using Koin

I'm trying to do a project in KMP, however, the app in the Android Version is working like a charm, but the iOS version crashes instantly.

Here is the error:

Uncaught Kotlin exception: org.koin.core.error.InstanceCreationException: Could not create instance for '[Factory: 'com.cdm.janaza.feature_main.presentation.screen.home.HomeViewModel']'

Here is my Koin setup:

fun initKoin(config: KoinAppDeclaration? = null) {
    startKoin {
        printLogger(Level.DEBUG)
        config?.invoke(this)
        modules(
            appModule,
            sharedRepositoryModule,
            viewModelModule
        )
    }
}

My MainViewController:

fun MainViewController() = ComposeUIViewController(
    configure = {
        initKoin()
        Firebase.initialize()
    }
) {
    App()
}

My ViewModelModule:

val viewModelModule = module {
    viewModelOf(::HomeViewModel)
}

My AppModule:

val appModule = module {
single { Firebase.firestore }
single { Firebase.messaging }
single { Firebase.auth }

single { createHttpClient() }

single(named("pray")) { providePrayerCollection(get()) }
single(named("user"))  { provideUserCollection(get()) }

singleOf(::OverlayServiceImpl).bind<OverlayService>()
singleOf(::BottomSheetServiceImpl).bind<BottomSheetService>()
singleOf(::ToastServiceImpl).bind<ToastService>()
singleOf(::BottomBarServiceImpl).bind<BottomBarService>()
singleOf(::AlertDialogServiceImpl).bind<AlertDialogService>()

singleOf(::PrayerServiceImpl).bind<PrayerService>()
singleOf(::UserServiceImpl).bind<UserService>()
singleOf(::AuthenticationServiceImpl).bind<AuthenticationService>()

single<FirebasePrayerRepository>{
    FirebasePrayerSourceImpl(
        prayerCollection = get(named("pray")),
        firebaseMessaging = get(),
        firebaseAuth = get()

    )
}
single<FirebaseUserRepository>{
    FirebaseUserSourceImpl(
        userCollection = get(named("user")),
        firebaseAuth = get()
    )
}
singleOf(::GooglePlacesRepositoryImpl).bind<GooglePlacesRepository>()
singleOf(::PrayersTimeRepositoryImpl).bind<PrayersTimeRepository>()
}

private fun providePrayerCollection(db: dev.gitlive.firebase.firestore.FirebaseFirestore) =
db.collection(if (isDebug) "dev_pray" else "pray")

private fun provideUserCollection(db: dev.gitlive.firebase.firestore.FirebaseFirestore) =
db.collection("user")

Source: View source

PressRex profile image
by PressRex

Subscribe to New Posts

Lorem ultrices malesuada sapien amet pulvinar quis. Feugiat etiam ullamcorper pharetra vitae nibh enim vel.

Success! Now Check Your Email

To complete Subscribe, click the confirmation link in your inbox. If it doesn’t arrive within 3 minutes, check your spam folder.

Ok, Thanks

Read More