Search code examples
androidkoin

NoParameterFoundException


I am trying to instantiate a Koin module for a viewModel with this constructor:

class CustomViewModel(
    displayData: DisplayData?,
    voterLocation: VoterLocation?,
    private val useLocalElections: Boolean,
    private val useLocalPollingPlaces: Boolean
)

I instantiate the viewModel module with this method

private fun customViewModel() = module {
    viewModel { (displayData: DisplayData?,
        voterLocation: VoterLocation?,
        useLocalElections: Boolean,
        useLocalPollingPlaces: Boolean) ->

        CustomViewModel(
            displayData,
            voterLocation,
            useLocalElections,
            useLocalPollingPlaces
        )
    }

}

and then declare it in my class with the "by" delegate:

private val viewModel: CustomViewModel by viewModel()

However, my error is telling me that one of the parameters for the displayData attribute can cannot be injected. Here is that class:

data class PoliticsHubElectionDisplayData(
    @DisplayDataTypeField(DisplayDataType.TYPE)
    override val type: DisplayDataType,
    @JsonRequired
    @SerializedName("general")
    val hubFormat:Format,
    @SerializedName("iconCard")
    val iconCards: ArrayList<IconCard?>?
)

And the error:

 Caused by: org.koin.core.error.NoParameterFoundException: Can't get injected parameter #0 from DefinitionParameters[] for type 'com.spectrum.spectrumnews.data.PoliticsHubElectionDisplayData'

How do I give this this class the parameter it needs?


Solution

  • You need to provide parameters to CustomViewModel. Docs and ex. private val viewModel by viewModel<CustomViewModel> { parametersOf(PoliticsHubElectionDisplayData(...), null, false, false) }.