Lucca Beurmann

    Lucca Beurmann

    11 months ago
    Hello everyone, i'm facing a problem with LazyVerticalGrids that causes my app to crash. I'm using a LazyGrid to display a list of photos (receiving the image url from an api and loading in my view using Glide) that can be cleared whenever the user sends a request with a differente filter, but when the list is cleared my application crashes with an Index out Of Bounds Exception
    Danish Ansari

    Danish Ansari

    11 months ago
    You yourself pointed out the actual problem. It doesn't have anything to do
    LazyVerticalGrid
    . Wherever you're using your list just put some kind of
    isEmpty()
    or
    contains()
    check according to your need
    Lucca Beurmann

    Lucca Beurmann

    11 months ago
    I'm already doing this check, only displaying the list if it's not empty
    if (photos.isEmpty()) {
        SearchPhotosEmpty(Modifier.align(Alignment.Center))
    } else {
        SearchPhotosList(
            photos = photos,
            listState = listState,
            onLoadMorePhotos = loadMorePhotos,
            isPreview = isPreview,
            modifier = Modifier.fillMaxSize()
        )
    }
    Chris Miller

    Chris Miller

    11 months ago
    Just a guess - is your use of
    photos
    threadsafe? And does the LazyGrid know to recompose when the photos change?
    Lucca Beurmann

    Lucca Beurmann

    11 months ago
    I think i found the source of this weird behavior
    I'm using a sealed class named ViewState to update my UI according to the proper view state of my screen. This class contains 4 differente types: Idle, Ready, Loading and Error
    when (screenState) {
        is ViewState.Idle -> {
            Column(
                modifier = Modifier
                    .fillMaxSize()
                    .padding(horizontal = 20.dp),
                horizontalAlignment = Alignment.CenterHorizontally,
                verticalArrangement = Arrangement.Center
            ) {
                Icon(
                    imageVector = Icons.Default.ImageSearch,
                    contentDescription = null,
                    modifier = Modifier
                        .size(60.dp)
                        .padding()
                        .padding(bottom = 10.dp),
                    tint = MaterialTheme.colors.onBackground
                )
                Text(
                    text = "Whoops nothing to see here yet, try to search for something cute like fluffy cats.",
                    style = AppTypoGraph.roboto_bold().copy(
                        fontSize = 18.sp,
                        textAlign = TextAlign.Center
                    )
                )
            }
        }
        is ViewState.Loading -> {
            Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
                ProgressIndicator()
            }
        }
        is ViewState.Ready -> Box(Modifier.fillMaxSize()) {
            if (photos.isEmpty()) {
                SearchPhotosEmpty(Modifier.align(Alignment.Center))
            } else {
                SearchPhotosList(
                    photos = photos,
                    listState = listState,
                    onLoadMorePhotos = loadMorePhotos,
                    isPreview = isPreview,
                    modifier = Modifier.fillMaxSize()
                )
                if (isMorePhotosLoading) MorePhotosLoader(
                    modifier = Modifier
                        .align(Alignment.BottomCenter)
                        .padding()
                        .padding(20.dp)
                        .zIndex(1f)
                )
            }
    
        }
        is ViewState.Error -> Unit
    }
    I'm using a when statement to set the correct layout and for some reason this is why the exception is occurring
    if i extract the LazyGrid of this when clausule and leave it unaware of the current view state it works as intended
    I'm just trying to understand why this is happening
    well actually this is not the cause of the problem
    i was using the .clear() before doing the api call
    i moved the clear() to the onSuccess method and it worked
    don't know why tho
    t

    Tobias Suchalla

    11 months ago
    You're probably seeing this:https://github.com/JetBrains/compose-jb/issues/1157 https://github.com/JetBrains/compose-jb/issues/1173 Build v1.0.0-alpha4-build331 was the last one without this issue.
    Andrey Kulikov

    Andrey Kulikov

    11 months ago
    please file a bug with the stacktrace. it shouldn’t crash