Koin: Allow nullable dependencies?

Created on 7 Feb 2018  路  4Comments  路  Source: InsertKoinIO/koin

Is it possible to provide an optional type?

I have application data that I'd like to inject, however the data isn't guaranteed at compile time and is fetched during runtime. Is it possible to inject a nullable type?

Currently I get the compile time error: inferred type User? is not a subtype of Any

Thanks in advance.

Most helpful comment

@arnaudgiuliani Is it something that could be added to Koin? My team members raised that issue lately. I understand the work around and was wondering why it was the case. It's not a technical limitation surely.

All 4 comments

I guess my question really is, is there a way to provide dependencies outside of the initial startKoin at the beginning? A way to create a new context only when it is needed?

Hello,

Koin doesn't provide any nullable inejction. I think that you could make a component that manage this behavior for you.

For example, you can have a ServiceManager:

ServiceManager(){

   var service : Service? = null

   fun createService(){
       // create your service
   }
}

And with Koin, inject the ServiceManager.

Thanks! I think I'm getting the hang of DI practices and such, this makes much more sense.

@arnaudgiuliani Is it something that could be added to Koin? My team members raised that issue lately. I understand the work around and was wondering why it was the case. It's not a technical limitation surely.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sankarsana picture sankarsana  路  4Comments

Jeevuz picture Jeevuz  路  4Comments

LukasAnda picture LukasAnda  路  3Comments

congtuyenvip picture congtuyenvip  路  4Comments

mubarak1361 picture mubarak1361  路  3Comments