I want to pass a list of objects to another composable screen through Typse safe navigation but I get this error:
Route com.home.ui.OwnerGraph.FirstLogin.FirstLoginScreen2 could not find any NavType for argument list of type kotlin.collections.ArrayList - typeMap received was {}
The destination:
composable<OwnerGraph.FirstLogin.FirstLoginScreen2>(
typeMap = mapOf(
typeOf<ArrayList<PropertyTagNavigation>>() to CustomNavType.List,
)
The navigation destination
@Serializable
data class FirstLoginScreen2(val list: ArrayList<PropertyTagNavigation>)
The object that being passed:
@Serializable
data class PropertyTagNavigation(
val id: Int,
val name: String,
)
NavType:
val List = object : NavType<ArrayList<PropertyTagNavigation>>(
isNullableAllowed = false
) {
override fun get(bundle: Bundle, key: String): ArrayList<PropertyTagNavigation>? {
return Json.decodeFromString(bundle.getString(key) ?: return null)
}
override fun parseValue(value: String): ArrayList<PropertyTagNavigation> {
return Json.decodeFromString(Uri.decode(value))
}
override fun serializeAsValue(value: ArrayList<PropertyTagNavigation>): String {
return Uri.encode(Json.encodeToString(value = value))
}
override fun put(bundle: Bundle, key: String, value: ArrayList<PropertyTagNavigation>) {
bundle.putString(key, Json.encodeToString(value))
}
}
The destination:
@HiltViewModel
class FirstLoginOwnerSecondScreenViewModel @Inject constructor(
savedStateHandle: SavedStateHandle,
) : ViewModel() {
private val _state = MutableStateFlow(
FirstLoginOwnerSecondScreenState(
regionPropertyList = savedStateHandle
.toRoute<OwnerGraph.FirstLogin.FirstLoginScreen2>().list.toPropertyTag()
)
)
....
}
And debugging it the savedStateHandle actually recieves the data but the error happens:
Inside the savedStateHandle :
list -> [{"id":1,"name":"xyz"},{"id":2,"name":"ffff"},.....]
You need pass typeMap parameter to savedStateHandle.toRoute() function
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With