zt
07/24/2024, 5:50 PMval tabs = mutableStateListOf(currentLocation)
var currentTabIndex by mutableIntStateOf(0)
fun newTab(path: Path = currentLocation) {
tabs.add(currentTabIndex, path)
currentTabIndex++
}
And in another composable I have
ScrollableTabRow(
modifier = Modifier.fillMaxWidth(),
selectedTabIndex = viewModel.currentTabIndex,
containerColor = MaterialTheme.colorScheme.surfaceColorAtElevation(3.dp),
edgePadding = 0.dp
) {
viewModel.tabs.forEachIndexed { index, title ->
The issue that happens is that adding a third tab results in it erroring Index 2 out of bounds for length 2
If I add a short delay between adding to the tabs list and updating the tab index then it works, but is there a proper way to solve this?Sergej Shafarenka
07/24/2024, 5:55 PMzt
07/24/2024, 7:37 PMArsildo Murati
07/24/2024, 7:39 PMzt
07/24/2024, 7:40 PMArsildo Murati
07/24/2024, 7:41 PMStylianos Gakis
07/24/2024, 10:02 PMSnapshot.withMutableSnapshot {}
if you really need to atomically update two unrelated states that don't actually fit inside one single object
https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:compose/[…]ose/runtime/snapshots/Snapshot.kt;l=431?q=withMutableSnapshotzt
07/24/2024, 10:06 PMselectedTabIndex = viewModel.currentTabIndex
Stylianos Gakis
07/24/2024, 10:10 PMScrollableTabRow
? Accessing the index itself shouldn't throw.
Does wrapping the state changes with a withMutableSnapshot
help here?zt
07/24/2024, 10:29 PMzt
07/25/2024, 3:43 AMfun newTab(path: Path = currentLocation) {
Snapshot.withMutableSnapshot {
tabs.add(currentTabIndex, path)
currentTabIndex++
}
}
zt
07/25/2024, 4:20 AMSergej Shafarenka
07/25/2024, 4:30 AMStylianos Gakis
07/25/2024, 7:53 AMzt
07/25/2024, 12:04 PMStylianos Gakis
07/25/2024, 12:20 PMzt
07/26/2024, 9:26 PMzt
07/26/2024, 9:27 PMDariana
09/18/2024, 2:26 PM