kotlin jetpack compose在从lazy列中删除项目时崩溃

jyztefdp  于 2023-11-21  发布在  Kotlin
关注(0)|答案(1)|浏览(177)

下面的代码在我从列表中删除一个项目时崩溃。我理解为什么会发生崩溃,这是因为索引超出范围错误java.lang.IndexOutOfBoundsException: Index 2, size 2,但我不知道在哪里实施修复,以便编译器知道更改。

@Composable
fun ManageHelpfulCotsView(
    coreDataListViewModel: CoreDataListViewModel,
    dataStoreVM: DataStoreViewModel,
    navController: NavController,
) {
    val showDetailSheet = remember { mutableStateOf(false) }

    val userHelpfulCotIDs =
        coreDataListViewModel.userProfileList.collectAsState(initial = listOf()).value?.filter { it.userName == coreDataListViewModel.profileName }

    val userHelpfulCots = coreDataListViewModel.barracksList.collectAsState(initial = listOf()).value?.filter {
        userHelpfulCotIDs?.firstOrNull { profile ->
            profile.helpfulCots?.contains(it.id) ?: false
        } != null
    }

    val userId = remember {
        mutableStateOf(
            runBlocking {
                dataStoreVM.hasUserProfileID()
            }
        )
    }

    LazyColumn(Modifier.fillMaxSize()) {
        val userHelpCots = userHelpfulCots ?: emptyList() // Ensure userHelpCots is not null

        itemsIndexed(userHelpCots) { index, barracksItem ->
                if (showDetailSheet.value) {
                    HelpfulSheet(
                        dataStoreVM = dataStoreVM,
                        coreDataModel = coreDataListViewModel,
                        navController = navController,
                        isHots = false
                    ) {
                        showDetailSheet.value = false
                    }
                }
                    BarracksEntryRowView(
                        qualityHousing = barracksItem,
                        navigationAction = {
                            showDetailSheet.value = true
                        },
                        coreDataViewModel = coreDataListViewModel,
                        userId = userId.value
                    )
                }
            }
    }

字符串
这是崩溃堆栈

FATAL EXCEPTION: main
   Process: com.rob.hotscots, PID: 25831
   java.lang.IndexOutOfBoundsException: Index 4, size 4
    at androidx.compose.foundation.lazy.layout.MutableIntervalList.checkIndexBounds(IntervalList.kt:183)
    at androidx.compose.foundation.lazy.layout.MutableIntervalList.get(IntervalList.kt:166)
    at androidx.compose.foundation.lazy.layout.LazyLayoutIntervalContent.getKey(LazyLayoutIntervalContent.kt:86)
    at androidx.compose.foundation.lazy.LazyListItemProviderImpl.getKey(LazyListItemProvider.kt:85)
    at androidx.compose.foundation.lazy.LazyListMeasuredItemProvider.getAndMeasure(LazyListMeasuredItemProvider.kt:46)

2lpgd968

2lpgd9681#

我能够通过更新一些vals来解决崩溃问题
val hotsList = coreDataListViewModel.dfacList.collectAsState(initial = listOf()).value
这里也
if (userHelpfulHotIDs.isNullOrEmpty() || userHelpfulHotIDs.firstOrNull()?.helpfulHots.isNullOrEmpty()) { EmptyView("No Helpful content found. Return here to see posts you've found helpful.") }
我还检查了
if (userHelpfulHotIDs?.first()?.helpfulHots?.contains(dfacItem.id) == true) {

相关问题