Search code examples
kotlinkoin

Change value with Koin injected variable


I need to assign a new value to my single Koin variable, but Koin does not allow to use var on the injected variable...

private val userAssets: UserAssets by inject()

How set value on userAssets? Or have another thing to do to make UserAssets Singleton?

val dataModule = module {
    factory {
        RetrofitBuilder(
            androidContext()
        )
    }
    single { LoginCredential() }
    single { UserAssets() }
}

Solution

  • I personally do not use Koin, so I can not verify it, but Koin also provides a way to directly retrieve an instance with get(). So in your case:

    private var userAssets: UserAssets = get()
    

    Might work so you can later reassign it, at least that is what I understand from their their documentation.