T
T
Tsuzukeru2021-06-07 13:47:31
Android
Tsuzukeru, 2021-06-07 13:47:31

How to pass parameter to Koin dependency graph?

Sometimes at runtime you need to pass your parameters to the dependency graph.
I'm trying to repeat the example on the documentation site.

I create a simple class with one property.
class A (val info:String)

I am making a module that will provide a singletone of this class, but with a parameter that I will pass at runtime.

val dataModule = module {
    single { params -> A(info = params.get()) }
}


Idea starts swearing that Type mismatch.Required:String Found: Any?
I tried to just copy the code from the documentation - the same error.
koin_version = "2.0.1"
What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis, 2021-06-21
@Tsuzukeru

val dataModule = module {
    // Привести к нужному типу явно
    single { params -> A(info = params.get() as String) }
    // или указать тип параметром типа у метода
    single { params -> A(info = params.get<String>()) }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question