Search code examples
androidioexceptionandroid-jetpack-datastore

Classifier 'IOException' does not have a companion object, and thus must be initialized here


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?


Solution

  • 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