I am working on a project that utilizes Jetpack Compose Navigation with Type-Safe Navigation. Within my application, I have an composable function responsible for hosting the navigation graph. Here’s a simplified version of the code:
@Composable
fun Content(
mainNavController: NavController = rememberNavController()
) {
val currentBackStackEntry by mainNavController.currentBackStackEntryAsState()
val currentRoute = currentBackStackEntry?.toRoute<Route>()
}
@Serializable
sealed interface Route {
@Serializable
data object Route1 : Route
@Serializable
data object Route2 : Route
@Serializable
data object Route3 : Route
}
I’m attempting to retrieve the current route object outside the composable<> block. However, I encounter the following exception:
IllegalArgumentException: Polymorphic value has not been read for class null
It appears that polymorphic behavior is not supported in this context. Can someone provide guidance on how to solve this issue? I need to be able to obtain the current route object outside the NavHost composable<> block using toRoute function. Thank you!