You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Jetpack Compose导航组件时遭遇Kotlin序列化MissingFieldException与SerializationException问题求助

Jetpack Compose导航组件时遭遇Kotlin序列化MissingFieldException与SerializationException问题求助

Hey there, let's break down these two serialization headaches you're facing with Jetpack Compose Navigation and Kotlinx Serialization, and fix them step by step!

Problem 1: MissingFieldException when removing default values from User data class

What's causing this?

When you strip out the default values from your User data class, Kotlinx Serialization expects every non-nullable field to be present in the serialized JSON. But your custom UserType NavConfig has encodeDefaults = false, which means fields set to their default values don't get included in the serialized output. So if you pass a User object where some fields use the old defaults (like an empty string for email), those fields won't show up in the JSON—and when deserializing, the serializer throws a fit because it can't find required fields.

Fixes you can try:

  1. Keep default values in User (simplest fix)
    If your use case allows it, leave the default values in the User data class. This way, even if a field is missing from the serialized JSON, Kotlinx Serialization will fill it in with the default value instead of throwing an error.

  2. Adjust your Json config to include defaults
    Change encodeDefaults to true in your UserType setup. This ensures every field (even those with default values) gets serialized, so deserialization will never miss required fields:

    val UserType = object : NavType<User>(isNullableAllowed = false) {
        private val json = Json {
            ignoreUnknownKeys = true
            isLenient = true
            encodeDefaults = true // Flip this to true
            explicitNulls = false
            prettyPrint = true
        }
        // Rest of your methods stay the same
    }
    
  3. Ensure all User fields are populated before navigation
    If you really don't want default values, make sure every non-nullable field in User has a valid value when you pass it to the Home destination. This way, all required fields will be present in the serialized JSON.


Problem 2: SerializationException about missing Companion serializer when removing default User from Home destination

What's causing this?

When you remove the default User value from the Home data class, the Jetpack Navigation component's serialization logic can get confused. It might accidentally try to serialize the Companion object associated with the Home class (which isn't marked @Serializable), leading to that error. Alternatively, your custom UserType might not play nicely with a destination that requires a mandatory parameter with no default.

Fixes you can try:

  1. Always pass a User explicitly when navigating to Home
    Make sure whenever you call navigate to go to the Home screen, you provide a valid User object—no exceptions. For example:

    navController.navigate(Destination.Home(user = currentLoggedInUser))
    
  2. Ditch the custom NavType and use built-in serialization
    Jetpack Navigation now has native support for Kotlinx Serialization, which can avoid these custom type issues entirely. Update your composable registration to let Navigation handle serialization automatically:

    // No need for typeMap anymore
    composable<Destination.Home> { backStackEntry ->
        val viewModel = koinViewModel<HomeViewModel>()
        val homeData = backStackEntry.toRoute<Destination.Home>()
        eVoteViewModel.setUser(homeData.user)
        val state by viewModel.state.collectAsStateWithLifecycle()
        HomeScreen(
            state = state,
            user = homeData.user,
            copyToClipboard = viewModel::copyToClipboard
        )
    }
    
  3. Double-check your sealed interface serialization
    Ensure your Destination sealed interface and all its subclasses are properly marked @Serializable. Sometimes, subtle issues with sealed type serialization can lead to strange Companion-related errors.


备注:内容来源于stack exchange,提问作者OmniCoder77

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.15 08:44:35