I'm using Androidx Navigation as a router in my app, whereas I have a bunch of different routes, and one has some arguments of the shape: route?$sectionArg={$sectionArg}&itemId={$itemId}. For example, a route for this navigation destination could be: myapp://catalog?section=books&itemId=69
I want the itemId argument to be "optional", i.e. that is OK to navigate to a route of the shape:
myapp://catalog?section=books
But when I define the arguments, I have to do this:
composable(
route = CatalogDestination.routeWithArgs,
arguments = listOf(
navArgument(CatalogDestination.sectionArg) {
type = NavType.StringType
defaultValue = "Books"
},
navArgument(PlantSheetDestination.usageArg) {
type = NavType.IntType
defaultValue = -1
}
),
Because, if I don't set a defaultValue I get a Runtime Error, similar to this one:
java.lang.IllegalArgumentException: Deep link myapp://catalog?section=Books can't be used to open destination Destination(0x0).
Following required arguments are missing: [itemId]
Nor I can set that argument as nullable, because the IntType for NavArgument doesn't allow it. In fact:
navArgument(PlantSheetDestination.usageArg) {
type = NavType.IntType
nullable = true
}
Throws:
java.lang.IllegalArgumentException: integer does not allow nullable value
Note that I'm not using safe args.