android / sunflower

A gardening app illustrating Android development best practices with migrating a View-based app to Jetpack Compose.
https://d.android.com/jetpack
Apache License 2.0
17.65k stars 4.71k forks source link

[ Fix ]: App Crash when scroll to page 2 in Gallery Screen #956

Closed teaim closed 2 months ago

teaim commented 7 months ago

When the first page is loaded, pageSize=75 https://api.unsplash.com/search/photos?query=Apple&page=1&per_page=75

When the second page is loaded, pageSize=25 https://api.unsplash.com/search/photos?query=Apple&page=2&per_page=25

Duplicate data on the first and second pages

FATAL EXCEPTION: main 
Process: com.google.samples.apps.sunflower, PID: 8802
java.lang.IllegalArgumentException: Key "P2X7NDx_GP0" was already used. If you are using LazyColumn/Row please make sure you provide a unique key for each item.

So App Crash when scroll to page 2 in Gallery Screen

Here's my solution, Specify the value of initialLoadSize explicitly

fun getSearchResultStream(query: String): Flow<PagingData<UnsplashPhoto>> {
      return Pager(
          config = PagingConfig(enablePlaceholders = false, pageSize = NETWORK_PAGE_SIZE, initialLoadSize = NETWORK_PAGE_SIZE),
          pagingSourceFactory = { UnsplashPagingSource(service, query) }
      ).flow
  }
google-cla[bot] commented 7 months ago

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

riggaroo commented 2 months ago

Hi, Thanks for your contribution, however Sunflower is going to be archived in favour of maintaining a smaller set of up-to-date set of compose-samples, that can be found here: https://github.com/android/compose-samples, as well as snippets that can be found here: https://github.com/android/snippets.

Please feel free to keep your fork up-to-date for your learning, but this repository will be archived shortly.