Jetpack compose navigation passing custom object with Kotlin DSL

I am trying type safety in Kotlin DSL and Navigation Compose but it looks like it is not working as expected, following is my code:

Screens:

@Serializable
data object SelectMenuNav

@Serializable
data class SelectProductNav(
    val menuSelection: MenuSelection
)

@Serializable
data class ProductDetailsNav(
    val menuSelection: MenuSelection,
    val product: Product
)

Custom Object I want to pass through composable

object CustomNavType {
    val ProductType = object : NavType<Product>(
        isNullableAllowed = false
    ) {
        override fun get(bundle: Bundle, key: String): Product? {
            return Json.decodeFromString(bundle.getString(key) ?: return null)
        }

        override fun parseValue(value: String): Product {
            return Json.decodeFromString(Uri.decode(value))
        }

        override fun put(bundle: Bundle, key: String, value: Product) {
            bundle.putString(key, Json.encodeToString(value))
        }

        override fun serializeAsValue(value: Product): String {
            return Uri.encode(Json.encodeToString(value))
        }
    }
}

My app navigation

@Composable
fun AppNavigation(modifier: Modifier = Modifier) {
    val navController = rememberNavController()
    NavHost(
        navController = navController,
        startDestination = SelectMenuNav
    ) {
        composable<SelectMenuNav> {
            ChooseMenuScreen(onSelection = { menuSelection ->
                navController.navigate(
                    SelectProductNav(
                        menuSelection = menuSelection
                    )
                )
            })
        }
        composable<SelectProductNav>(
            typeMap = mapOf(
                typeOf<MenuSelection>() to NavType.EnumType(MenuSelection::class.java)
            )
        ) {
            val menuSelection = it.toRoute<SelectProductNav>().menuSelection
            ChooseProductScreen(
                menuSelection = menuSelection,
                viewModel = hiltViewModel(),
                onNavigateToProductDetails = { product ->
                    navController.navigate(
                      ProductDetailsNav(
                          product = product,
                          menuSelection = menuSelection
                      )
                    )
                })
        }
        composable<ProductDetailsNav>(
            typeMap = mapOf(
                typeOf<Product>() to CustomNavType.ProductType,
                typeOf<MenuSelection>() to NavType.EnumType(MenuSelection::class.java)
            )
        ) { backStackEntry ->
            Log.d("AppNavigation", "ProductDetailsNav: ${backStackEntry.toRoute<ProductDetailsNav>()}")
            ProductDetailsScreen(
                modifier = Modifier,
                vm = hiltViewModel(),
                navigateToEditor = {},
                navigateBack = {}
            )
        }
    }
}

Even though Log is fine showing the product that has passed successfully between composables:

D  ProductDetailsNav: ProductDetailsNav(menuSelection=NAVA, product=Product(id=4371, productName=[...]

My viewmodel throws an error:

  java.lang.IllegalArgumentException: Route app.xml.aionianew.navigation.ProductDetailsNav could not find any NavType for argument product of type app.xml.aionianew.database.internal.data.Product - typeMap received was {}
                                                                                                        at androidx.navigation.serialization.RouteSerializerKt$generateNavArguments$2$1.invoke(RouteSerializer.kt:108)
                                                                                                        at androidx.navigation.serialization.RouteSerializerKt$generateNavArguments$2$1.invoke(RouteSerializer.kt:103)
                                                                                                        at androidx.navigation.NamedNavArgumentKt.navArgument(NamedNavArgument.kt:21)
                                                                                                        at androidx.navigation.serialization.RouteSerializerKt.generateNavArguments(RouteSerializer.kt:103)
                                                                                                        at androidx.navigation.SavedStateHandleKt.internalToRoute(SavedStateHandle.kt:50)
                                                                                                        at app.xml.aionianew.screens.product_details.ViewModelProductDetails.<init>(ViewModelProductDetails.kt:1018)
                                                                                                        at app.xml.aionianew.DaggerMyApp_HiltComponents_SingletonC$ViewModelCImpl$SwitchingProvider.get(DaggerMyApp_HiltComponents_SingletonC.java:497)
                                                                                                        at dagger.hilt.android.internal.lifecycle.HiltViewModelFactory$2.createViewModel(HiltViewModelFactory.java:133)

My code in viewmodel is like so:

@HiltViewModel
class ViewModelProductDetails @Inject constructor(
    private val uc: UseCasesProductDetails,
    savedState: SavedStateHandle
) : ViewModel() {

    private val selectedProduct: Product = savedState.toRoute<ProductDetailsNav>().product
    private val menuSelection:MenuSelection = savedState.toRoute<ProductDetailsNav>().menuSelection

So the question is what am I missing…

savedState.toRoute also takes a typeMap – without that typeMap, it doesn’t know how to convert the SavedStateHandle back into your object so you’ll want to pass in the same typeMap as you used in your navigation graph definition in your ViewModel:

private val productDetails = savedState.toRoute<ProductDetailsNav>(
    typeMap = mapOf(
        typeOf<Product>() to CustomNavType.ProductType,
        typeOf<MenuSelection>() to NavType.EnumType(MenuSelection::class.java)
    )
)
private val selectedProduct: Product = productDetails.product
private val menuSelection: MenuSelection = productDetails.menuSelection

Recognized by Mobile Development Collective

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật