- Когда я удаляю значения по умолчанию из класса данных пользователя, я получаю:
kotlinx.serialization.MissingFieldException: Fields [email, displayName, authToken, voterId, candidateId, adminId] are required for type with serial name
- Когда я удаляю значение пользователя по умолчанию из пункта назначения «Дом», я получаю:
Please ensure that class is marked as '@Serializable' and that the serialization compiler plugin is applied.
Вот мой соответствующий код:
@SuppressLint("UnsafeOptInUsageError")
@Serializable
sealed interface Destination {
// Other destinations...
@Serializable
data class Home(val user: User = User( // Absence of this is gives Missing Field exception
email = "",
displayName = "New User",
authToken = "",
voterId = 0,
candidateId = 0,
adminId = 0,
profilePicture = null
)) : Destination
}
@Serializable
data class User(
val email: String = "",
val displayName: String = "",
val authToken: String = "",
val voterId: Int? = null,
val candidateId: Int? = null,
val adminId: Int? = null,
val profilePicture: String?
)
composable(
typeMap = mapOf(typeOf() to UserType)
) { backStackEntry ->
val viewModel = koinViewModel()
val homeData: Destination.Home = backStackEntry.toRoute()
eVoteViewModel.setUser(homeData.user)
val state by viewModel.state.collectAsStateWithLifecycle()
HomeScreen(
state = state,
user = homeData.user,
copyToClipboard = viewModel::copyToClipboard
)
}
val UserType = object : NavType(isNullableAllowed = false) {
private val json = Json {
ignoreUnknownKeys = true
isLenient = true
encodeDefaults = false
explicitNulls = false
prettyPrint = true
}
override fun get(bundle: Bundle, key: String): User {
return json.decodeFromString(bundle.getString(key)!!)
}
override fun parseValue(value: String): User {
return json.decodeFromString(Uri.decode(value))
}
override fun serializeAsValue(value: User): String {
return Uri.encode(
json.encodeToString(
serializer = User.serializer(), value = value
)
)
}
override fun put(bundle: Bundle, key: String, value: User) {
bundle.putString(key, json.encodeToString(value))
}
}
Подробнее здесь: https://stackoverflow.com/questions/791 ... and-defaul
Мобильная версия