From f5a52ca01b1b08cefbaec566f6df2935304628e4 Mon Sep 17 00:00:00 2001 From: Andrea Bizzotto Date: Fri, 12 Apr 2024 11:59:04 +0100 Subject: [PATCH] Cleanup MoviesSearchScreen --- .../movies/movies_search_screen.dart | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/lib/src/features/movies/presentation/movies/movies_search_screen.dart b/lib/src/features/movies/presentation/movies/movies_search_screen.dart index 6ae1bf0..8ae0c43 100644 --- a/lib/src/features/movies/presentation/movies/movies_search_screen.dart +++ b/lib/src/features/movies/presentation/movies/movies_search_screen.dart @@ -28,16 +28,23 @@ class MoviesSearchScreen extends ConsumerWidget { const MoviesSearchBar(), Expanded( child: RefreshIndicator( - onRefresh: () { + onRefresh: () async { // dispose all the pages previously fetched. Next read will refresh them ref.invalidate(fetchMoviesProvider); // keep showing the progress indicator until the first page is fetched - return ref.read( - fetchMoviesProvider(queryData: (page: 1, query: query)) - .future, - ); + try { + await ref.read( + fetchMoviesProvider(queryData: (page: 1, query: query)) + .future, + ); + } catch (e) { + // fail silently as the provider error state is handled inside the ListView + } }, child: ListView.builder( + // use a different key for each query, ensuring the scroll + // position is reset when the query and results change + key: ValueKey(query), // * pass the itemCount explicitly to prevent unnecessary renders // * during overscroll itemCount: totalResults, @@ -69,7 +76,7 @@ class MoviesSearchScreen extends ConsumerWidget { final movie = response.results[indexInPage]; return MovieListTile( movie: movie, - debugIndex: index, + debugIndex: index + 1, onPressed: () => context.goNamed( AppRoute.movie.name, pathParameters: {'id': movie.id.toString()}, @@ -117,10 +124,10 @@ class MovieListTileError extends ConsumerWidget { onPressed: isLoading ? null : () { - // dispose all the pages previously fetched. Next read will refresh them + // invalidate the provider for the errored page ref.invalidate(fetchMoviesProvider( queryData: (page: page, query: query))); - // keep showing the progress indicator until the first page is fetched + // wait until the page is loaded again return ref.read( fetchMoviesProvider( queryData: (page: page, query: query)).future,