NoParameterFoundException异常

NoParameterFoundException

我正在尝试使用此构造函数为 viewModel 实例化 Koin 模块:

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

我用这个方法实例化了 viewModel 模块

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

        CustomViewModel(
            displayData,
            voterLocation,
            useLocalElections,
            useLocalPollingPlaces
        )
    }

}

然后在我的 class 中用“by”代表声明它:

private val viewModel: CustomViewModel by viewModel()

但是,我的错误告诉我无法注入 displayData 属性的参数之一。这是 class:

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

错误:

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

如何给这个class它需要的参数?

您需要向 CustomViewModel 提供参数。 Docs 和前。 private val viewModel by viewModel<CustomViewModel> { parametersOf(PoliticsHubElectionDisplayData(...), null, false, false) }.