分类器' ioexception'没有伴侣对象,因此必须在此处初始化
我正在尝试从偏好数据存储
中读取数据并遇到此意外错误。
这是Google link1
这是官方文档 link2
private const val DATA_STORE_NAME = "SETTINGS_STATE_OF_APP"
val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = DATA_STORE_NAME)
class PreferencesDatastore(private val context: Context) {
val readData: Flow<Model> = context.dataStore.data
.catch { exception ->
if (exception in IOException){ // here the IOException is throwing error
emit(emptyPreferences())
}else {
throw exception
}
}
.map {
}
}
同样的代码在一个较旧的项目中正常工作。
谁能告诉我如何解决这个问题?
I am trying to read data from preference datastore
and getting this unexpected error.
Here's a codelab from google link1
Here's the official doc link2
private const val DATA_STORE_NAME = "SETTINGS_STATE_OF_APP"
val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = DATA_STORE_NAME)
class PreferencesDatastore(private val context: Context) {
val readData: Flow<Model> = context.dataStore.data
.catch { exception ->
if (exception in IOException){ // here the IOException is throwing error
emit(emptyPreferences())
}else {
throw exception
}
}
.map {
}
}
This same code is working fine in an older project.
Can anyone tell me how to solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于使用在关键字中使用
进行测试,如果对象为给定类型而不是
是
。为了防止此类问题,我建议您检查 type noreferrer“ java实例运算符The problem was in using
in
keyword for testing if an object is of a given type instead ofis
. To prevent these kind of problems in future I recommed you to check Type checks and casts in Kotlin and Java instanceOf Operator