Home > other >  Conditional navigation start destination in jetpack compose
Conditional navigation start destination in jetpack compose

Time:07-20

I am using navigation-compose along with bottom bar in jetpack compose. I want to show different bottom bar for different type of user. For that, I need to set conditional startDestination in NavHost. How do i do that?

I've tried below but it change startDestination but does not reflect in UI.

val user by remember { mutableStateOf(viewModel.user) }.collectAsState()
var startDestination = Screens.UserType1.route

LaunchedEffect(user) {
    startDestination = if (loggedInUser?.userType == UserType.Seller) Screens.SellerHome.route else Screens.BuyerHome.route
}

While below code throws java.util.NoSuchElementException: List contains no element matching the predicate.

when (user?.userType) {
    UserType.Seller -> {
        startDestination = Screens.SellerHome.route
    }

    UserType.Buyer -> {
        startDestination = Screens.BuyerHome.route
    }

    else -> {}
}

My NavHost

NavHost(
        modifier = modifier,
        navController = navController,
        startDestination = startDestination
) {
    ...
}

CodePudding user response:

Change this

var startDestination = Screens.UserType1.route

To this

var startDestination by remember { mutableStateOf(Screens.UserType1.route) } 
  • Related