Sometimes you can't declare only components via Koin. Dependening on your runtime technology, you might need to retrieve instances from Koin in a class that was not created with Koin (e.g. Flutter Widgets)
The KoinComponent interface
Tag your class with the
KoinComponent interface to unlock Koin injection features:
inject()- lazy inject an instance
get()- retrieve an instance
We can inject the module above into class properties:
If the class already inherits another one you can use KoinComponentMixin instead of inheriting KoinComponent.
And we just need to start Koin and run our class:
KoinComponentinterface is also used to help you bootstrap an application from outside of Koin. Also, you can bring
KoinComponentfeature by extension functions directly on some target classes. The koin_flutter pachage use extensions to provide the Koin API for Flutter Widgets.
Bridge with Koin instance
KoinComponent interface brings the following:
It opens the following possibilties:
You can then redefine then
getKoin()function to redirect to a local custom Koin instance