首页 > 解决方案 > 如何获得 KProperty<*>?

问题描述

这是代码:

1. private val dataStore: Lazy<ReadOnlyProperty<Context, DataStore<Preferences>>> = lazy {
    preferencesDataStore(Objects.DATA_STORE_NAME)
}
2. val dataStoreValue: ReadOnlyProperty<Context, DataStore<Preferences>> = dataStore.value
3. val dataStoreGotValue: DataStore<Preferences> = dataStoreValue.getValue(requireContext(), "what it wants?")

第 3 点导致问题“Type mismatch.Required:KProperty<*> Found:String”

我试过这样做:“val prop: KProperty<*> = object : KProperty {...

            override fun call(vararg args: Any?): String {return "ok"}

            override fun callBy(args: Map<KParameter, Any?>): String { return "ok"}

}”,它确实有效,但它会导致过多的冗余代码。

有没有办法接收一些随机的 KProperty<*> 塞进“dataStoreValue.getValue”的嘴里,最终得到我的 DataStore,它已经存在了?

标签: androidkotlindatastore

解决方案


推荐阅读