# ItemSnapshotListFlow

Added in 3.5.3

*** ** * ** ***


```
public final class ItemSnapshotListFlow
```

<br />

*** ** * ** ***

## Summary

| ### Public methods |
|---|---|
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/paging/ItemSnapshotList<@https://developer.android.com/reference/androidx/annotation/NonNull T>>` | `<T extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/paging/ItemSnapshotListFlow#(kotlinx.coroutines.flow.Flow).asItemSnapshotListFlow(kotlin.Function1)( @https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/paging/PagingData<@https://developer.android.com/reference/androidx/annotation/NonNull T>> receiver, @https://developer.android.com/reference/androidx/annotation/NonNull Function1<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/paging/CombinedLoadStates, https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html> onLoadError )` Converts a Flow of `https://developer.android.com/reference/androidx/paging/PagingData` into a Flow of `https://developer.android.com/reference/androidx/paging/ItemSnapshotList`. |

## Public methods

### asItemSnapshotListFlow

Artifact: [androidx.paging:paging-common](https://developer.android.com/jetpack/androidx/releases/paging) [View Source](https://cs.android.com/search?q=file:androidx/paging/ItemSnapshotListFlow.kt+function:asItemSnapshotListFlow) Added in [3.5.3](https://developer.android.com/jetpack/androidx/releases/paging#3.5.3)

```
public static final @NonNull Flow<@NonNull ItemSnapshotList<@NonNull T>> <T extends Object> asItemSnapshotListFlow(
    @NonNull Flow<@NonNull PagingData<@NonNull T>> receiver,
    @NonNull Function1<@NonNull CombinedLoadStates, Unit> onLoadError
)
```

Converts a Flow of `https://developer.android.com/reference/androidx/paging/PagingData` into a Flow of `https://developer.android.com/reference/androidx/paging/ItemSnapshotList`. An emitted `https://developer.android.com/reference/androidx/paging/ItemSnapshotList` contains a snapshot of all loaded data and can be cached for repeated reads.

To reflect the latest snapshot, this flow emits a new `https://developer.android.com/reference/androidx/paging/ItemSnapshotList` whenever new items are loaded or whenever loaded items are dropped (i.e. to fulfill `https://developer.android.com/reference/androidx/paging/PagingConfig#maxSize()`).

The flow is kept active as long as the collection scope is active. To avoid leaks, make sure to use a scope that is already managed (like a ViewModel scope) or manually cancel it when you don't need paging anymore.

To use this flow with multiple collectors, convert it to a SharedFlow with Flow operators such as stateIn() or sharedIn().

Note that this Flow remains as a cold flow and does not start any loading until collected upon.

`https://developer.android.com/reference/androidx/paging/package-summary#(kotlinx.coroutines.flow.Flow).asItemSnapshotListFlow(kotlin.Function1)` - the paged item type

```kotlin
import androidx.compose.foundation.lazy.items
import androidx.lifecycle.ViewModel
import androidx.paging.ItemSnapshotList
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingSource
import androidx.paging.asItemSnapshotListFlow

lateinit var pagingSourceFactory: () -> PagingSource<String, SampleData.Item>

/** Initialize Pager in ViewModel */
val pager =
    Pager(config = PagingConfig(pageSize = 40), pagingSourceFactory = pagingSourceFactory)

val itemsFlow = pager.flow.asItemSnapshotListFlow()
```

```kotlin
import androidx.compose.foundation.lazy.items
import androidx.lifecycle.ViewModel
import androidx.paging.ItemSnapshotList
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingSource
import androidx.paging.asItemSnapshotListFlow
import androidx.paging.insertSeparators

lateinit var pagingSourceFactory: () -> PagingSource<String, SampleData.Item>

/** Initialize Pager in ViewModel */
val pager =
    Pager(config = PagingConfig(pageSize = 40), pagingSourceFactory = pagingSourceFactory)

val itemsFlow: Flow<ItemSnapshotList<SampleData>> =
    pager.flow
        /**
         * To improve performance, apply mapping and transformations on [Pager.flow] before
         * calling asState() to ensure that transforms are executed incrementally on newly
         * loaded data.
         */
        .map { pagingData ->
            pagingData.insertSeparators { _, next ->
                if (next?.value?.rem(10) == 0) SampleData.Separator(next.value.toString())
                else null
            }
        }
        .asItemSnapshotListFlow()
```

```kotlin
import androidx.compose.foundation.lazy.items
import androidx.lifecycle.ViewModel
import androidx.paging.ItemSnapshotList
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingSource
import androidx.paging.asItemSnapshotListFlow

lateinit var pagingSourceFactory: () -> PagingSource<String, SampleData.Item>

/** Initialize Pager in ViewModel */
val pager =
    Pager(config = PagingConfig(pageSize = 40), pagingSourceFactory = pagingSourceFactory)
val itemsFlow =
    pager.flow.asItemSnapshotListFlow { _ ->
        /** Retries the failed load without invalidating the PagingSource */
        pager.retry()
    }
```

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull Function1<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/paging/CombinedLoadStates, https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html> onLoadError` | the callback invoked when any loads return `https://developer.android.com/reference/androidx/paging/LoadState.Error`. Provides the `https://developer.android.com/reference/androidx/paging/CombinedLoadStates` containing the error. No-op by default. See `https://developer.android.com/reference/androidx/paging/Pager#retry()` for a recovery option. |