Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dependency injection inside extension function

Is there a way to inject an object inside an extension function or a global function using DI framework in Android Kotlin?

I use this function in many places. So I do not want to pass a parameter every time.

DI framework can be any of Koin, Hilt, Dagger2, or others.

Something like that:

fun Context.showSomething() {
 val myObject = inject()
 showToast(myObject.text)
}
like image 780
Yeldar Nurpeissov Avatar asked Jul 11 '26 19:07

Yeldar Nurpeissov


1 Answers

Instead of thinking about using Inject you could pass it as a parameter:

fun Context.showSomething(myObject: String) {
 showToast(myObject.text)
}
like image 91
DoctorWho Avatar answered Jul 14 '26 10:07

DoctorWho



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!