Search code examples
androidkotlinandroid-jetpack-composeandroid-navigationandroid-navigation-graph

No value passed for parameter 'route'


I am trying to learn app navigation with jetpack compose. I was reading and going through the documentation and code snippets and I came across this code here (Link).

In the documentation, they have mentioned that we need to use a serializable object or class to define a route. I defined the routes as they mentioned in the code snippet and got the following error.

No value passed for parameter 'route' in androidx.navigation.compose.composable

This is my MainActivity.kt file

package com.example.navigationsample

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.runtime.Composable
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import kotlinx.serialization.Serializable
import androidx.navigation.compose.rememberNavController
import com.example.navigationsample.ui.theme.NavigationSampleTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            NavigationSampleTheme {
                MyApp()
            }
        }
    }
}

@Serializable
object Home

@Serializable
object Profile

@Composable
fun MyApp() {

    // to create a NavController, call rememberNavController()
    val navController: NavHostController = rememberNavController()

    // NavHost is the UI component which swaps in and out the screens
    NavHost(navController, startDestination = Home) {
        composable<Home> {
            HomeScreen(onNavigateToProfile = { id ->
                navController.navigate(Profile(id))
            })
        }
        composable<Profile> { backStackEntry ->
            val profile: Profile = backStackEntry.toRoute()
            ProfileScreen(profile)
        }
    }
}

Solution

  • the documentation can use serializable object because it's the newer version of jetpack compose navigation 2.8.0-beta05. Below that you can only use string.

    Hope this help