I have an app with Koin where most injection happe...
# koin
z
I have an app with Koin where most injection happens on the ViewModel level with the Compose APIs, but there's also a
BroadcastReceiver
that I need to inject at some point. As my Koin context is set up at the Compose entry point, it might not have been initialized when this
BroadcastReceiver
is invoked. So far I'm doing this, but wondering if it could be cleaner:
Copy code
val logger = KoinPlatformTools.defaultContext().getOrNull()?.getOrNull<Logger>()
👀 1
a
Hey Márton, it's a component for Android only right? You would need to use it in that case?
z
Yes, this is specifically in the Android app
a
Ok, then you have all your stuff in commonMain, but you have a case where you need to retrieve the BroadcastReceiver "by hand", in the android part. Use the "by inject", or
KoinPlatform.getKoin().get<BroadcastReceiver>()
or or your snippet
KoinPlatform.getKoin().getOrNull<Logger>()
z
Where does
by inject
come from, which dependency? It seems like I might not have that. The one I have included so far is
koin-compose-viewmodel-navigation
.
a
by inject()
is available bye default in Android classes, or if you use
KoinComponent
interface
Else, you can use
KoinPlatform
API