分类器' ioexception'没有伴侣对象,因此必须在此处初始化

发布于 2025-02-13 16:08:21 字数 979 浏览 0 评论 0原文

我正在尝试从偏好数据存储中读取数据并遇到此意外错误。

这是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黒涩兲箜 2025-02-20 16:08:21

问题在于使用在关键字中使用进行测试,如果对象为给定类型而不是。为了防止此类问题,我建议您检查 type noreferrer“ java实例运算符

The problem was in using in keyword for testing if an object is of a given type instead of is. To prevent these kind of problems in future I recommed you to check Type checks and casts in Kotlin and Java instanceOf Operator

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文