0

我有一个显示过滤器选项的过滤器对话框:

在此处输入图像描述

当我单击“父平台”部分时,我将导航到PlatformsScreen带有两个参数的。参数一个是parentint ,第二个是平台,它是一个 int 数组:

sealed class Screen(val route: String, val arguments: List<NamedNavArgument>) {

    object PlatformScreen : Screen(PLATFORM_ROUTE,arguments = 
        listOf(navArgument("parent", builder = {type = NavType.IntType}),
        navArgument("platforms",builder = {type = NavType.IntArrayType})))
}


// click action
@Composable
fun FilterDialog(
    modifier: Modifier = Modifier,
    viewModel: FilterViewModel,
    navigateToPlatformScreen: (Int,Array<Int?>) -> Unit
) {
 val filterState by viewModel.filterState.collectAsState()
 Row(//other stuff
       .clickable {
                    navigateToPlatformScreen(filterState.parentPlatform?.id ?: -1,
                    filterState.platforms?.map { it?.id }?.toTypedArray() ?: arrayOf(-1))
                }
)
  //here is my filter bottom sheet
   bottomSheet(route = FILTER_ROUTE) {
        FilterDialog(viewModel = viewModel,
            navigateToPlatformScreen = { parentId,platforms ->
                navController.navigate("${Screen.PlatformScreen.route}?parent=${parentId}&platforms=${platforms}")
            })
    }

    //platforms screen
    bottomSheet(route = Screen.PlatformScreen.route+"?parent={parent}&platforms={platforms}",arguments = Screen.PlatformScreen.arguments) { backstackEntry ->
        val parent = backstackEntry.arguments?.getInt("parent")
        val platforms = backstackEntry.arguments?.getIntArray("platforms")
        PlatformScreen(parent = parent,platforms = platforms)
    }  

作为最后一部分,这里是 myFilterViewModel包含 myparentIdplatformsarray :

class FilterViewModel : ViewModel() {

    private val _selectedParentPlatform = MutableStateFlow<ParentPlatform?>(ParentPlatform())

    private val _filterState = MutableStateFlow(FilterState())

    val filterState:StateFlow<FilterState> get() = _filterState

    init {
        viewModelScope.launch {
           _selectedParentPlatform.mapLatest {
               FilterState(
                   parentPlatform = it,
                   platforms = it?.platforms
               )
           }
                .catch { cause ->

                }
                .collectLatest {
                    _filterState.value = it
                }
        }
    }
}

parent我的论点没有问题。我可以毫无问题地传递它,但是数组类型导致崩溃并出现以下错误: java.lang.UnsupportedOperationException: Arrays don't support default values.

4

0 回答 0