I have the following SharedFlow in my viewmodel -
class HeroesViewModel(private val heroesRepositoryImpl: HeroesRepositoryImpl) : ViewModel() {
private val _uiState = MutableStateFlow(UiState())
val uiState = _uiState.asStateFlow()
private val _uiAction = MutableSharedFlow<UiAction>()
val uiAction = _uiAction.asSharedFlow()
sealed class UiAction {
data class NavigateToHeroesDetails(val heroModel: HeroModel) : UiAction()
}
And I implement the observing of it in my Composable screen -
fun DashboardScreen(
navigator: DestinationsNavigator,
viewModel: HeroesViewModel = get()
) {
val uiState by viewModel.uiState.collectAsState()
val uiAction by viewModel.uiAction.collectAsState(initial = null)
when (uiAction) {
is HeroesViewModel.UiAction.NavigateToHeroesDetails -> {
navigator.navigate(HeroDetailsScreenDestination(uiAction.heroModel)) // Here is where I get the compiler error
}
null -> Unit
}
When trying to use the information from the action, the compiler gives me the following error -
Smart cast to 'HeroesViewModel.UiAction.NavigateToHeroesDetails' is impossible, because 'uiAction' is a property that has open or custom getter
What would be the correct way verify that the variable is indeed of the type I want?