最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

android kotlin `when` statement not smart casting - Stack Overflow

programmeradmin0浏览0评论

I have the following state sealed interface

sealed interface MainState {
    data class Success(
        val hasUsername: Boolean = false,
        val isSessionActive: Boolean = false) : MainState

    data object Loading : MainState
}

and my MainViewModel

 private val _mainState = MutableStateFlow(MainState.Loading)
    val mainState = _mainState.asStateFlow()
        .onStart {
            if(!hasCompleted) {
                getLoginCredentials()
                hasCompleted = true
            }

then in my composable

 val mainState by mainViewModel.mainState.collectAsStateWithLifecycle(initialValue = MainState.Loading)
       
        when(val status = mainState) {
           MainState.Loading -> {
                Box(
                    modifier = Modifier.fillMaxSize(),
                    contentAlignment = Alignment.Center
                ) {
                    CircularProgressIndicator()
                }
            }
            is MainState.Success -> {
                NavHost(
                    navController = navController,
                    startDestination = if(status.isSessionActive) Route.DashboardGraph else Route.AuthenticationGraph
                ) {

                    this.authentication(navController)

                    this.dashboardGraph(navController)

                    this.settingsGraph(navController)
                }
            }
        }
            }

And I am getting the following error at this line is MainState.Success -> {

This is the error Incompatible types: MainState. Success and MainState. Loading

I have the following state sealed interface

sealed interface MainState {
    data class Success(
        val hasUsername: Boolean = false,
        val isSessionActive: Boolean = false) : MainState

    data object Loading : MainState
}

and my MainViewModel

 private val _mainState = MutableStateFlow(MainState.Loading)
    val mainState = _mainState.asStateFlow()
        .onStart {
            if(!hasCompleted) {
                getLoginCredentials()
                hasCompleted = true
            }

then in my composable

 val mainState by mainViewModel.mainState.collectAsStateWithLifecycle(initialValue = MainState.Loading)
       
        when(val status = mainState) {
           MainState.Loading -> {
                Box(
                    modifier = Modifier.fillMaxSize(),
                    contentAlignment = Alignment.Center
                ) {
                    CircularProgressIndicator()
                }
            }
            is MainState.Success -> {
                NavHost(
                    navController = navController,
                    startDestination = if(status.isSessionActive) Route.DashboardGraph else Route.AuthenticationGraph
                ) {

                    this.authentication(navController)

                    this.dashboardGraph(navController)

                    this.settingsGraph(navController)
                }
            }
        }
            }

And I am getting the following error at this line is MainState.Success -> {

This is the error Incompatible types: MainState. Success and MainState. Loading

Share Improve this question asked Mar 14 at 15:20 ant2009ant2009 22.7k167 gold badges436 silver badges645 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Try replacing:

private val _mainState = MutableStateFlow(MainState.Loading)

with:

private val _mainState: MutableStateFlow<MainState> = MutableStateFlow(MainState.Loading)

or:

private val _mainState = MutableStateFlow<MainState>(MainState.Loading)

Kotlin's type inference is cool but not magic. In your original formulation, it inferred that the type was MutableStateFlow<MainState.Loading>. Since MainState.Success is not a subtype of MainState.Loading, your is MainState.Success branch was deemed to be impossible. We have to teach the compiler that you really wanted the supertype, despite initializing it to the subtype.

发布评论

评论列表(0)

  1. 暂无评论