I am totally new in sealed Interface
in kotlin. I am trying to state management through sealed
in android kotlin. My main goal is when
I used the object of sealed
i don't want to inherit all child. I am not sure correctly is that sealed interface
is right choice for me. All my code may be wrong, please correct if I am wrong Thanks.
sealed interface ConnectionUIState
sealed class BluetoothConnectionUIState {
object Initial : BluetoothConnectionUIState()
data class ScanningDevice(val storedDevice: SnapshotStateList<BluetoothDevice>? = null) : ConnectionUIState
}
I initialise the variable like this
var uiState by mutableStateOf<BluetoothConnectionUIState>(BluetoothConnectionUIState.Initial)
private set
Now I am passing the uiState
variable in the function and using when
statement
when (uiState) {
BluetoothConnectionUIState.ScanningDevice -> {
xuz()
}
}
why when
statement is giving error
'when' expression must be exhaustive, add necessary 'Initial' branch or 'else' branch instead
Also this line is also giving me error BluetoothConnectionUIState.ScanningDevice
in when
statement.
Error
Classifier 'ScanningDevice' does not have a companion object, and thus must be initialized here
If I am doing wrong here. Can you please elaborate of 2 point of this stack overflow. Thanks
UPDATE
I did some changes
sealed interface ConnectionUIState
sealed class BluetoothConnectionUIState {
object Initial : ConnectionUIState
data class ScanningDevice(val storedDevice: SnapshotStateList<BluetoothDevice>? = null) : BluetoothConnectionUIState()
}
I did success on when
statement that it's not complaining about Initial
when (uiState) {
is BluetoothConnectionUIState.ScanningDevice -> {
BluetoothPairContent(viewModel, tryAgainAction, openSettingAction, scanDeviceList)
}
}
That is my goal, but another problem raised that it gives error in uiState
initialise time
var uiState by mutableStateOf<BluetoothConnectionUIState>(BluetoothConnectionUIState.Initial)
private set
Error
Type mismatch.
Required:
BluetoothConnectionUIState
Found:
BluetoothConnectionUIState.Initial
Again I am confused. Please guide me on this. Thanks