From c856953f31579c761f7553c24260bc47f6cfd4e0 Mon Sep 17 00:00:00 2001 From: John Wesley Date: Sun, 21 Jan 2024 12:01:50 -0500 Subject: [PATCH] Fix duplicate items and unmounted pages for pagination --- lib/src/screens/entries/entries_list.dart | 13 +++++++++-- lib/src/screens/entries/entry_page.dart | 22 +++++++++++++++---- lib/src/screens/explore/domains_screen.dart | 13 +++++++++-- lib/src/screens/explore/magazines_screen.dart | 13 +++++++++-- lib/src/screens/explore/users_screen.dart | 13 +++++++++-- lib/src/screens/posts/post_page.dart | 22 +++++++++++++++---- lib/src/screens/posts/posts_list.dart | 13 +++++++++-- .../screens/profile/notification_screen.dart | 13 +++++++++-- 8 files changed, 102 insertions(+), 20 deletions(-) diff --git a/lib/src/screens/entries/entries_list.dart b/lib/src/screens/entries/entries_list.dart index c877039..8ebe432 100644 --- a/lib/src/screens/entries/entries_list.dart +++ b/lib/src/screens/entries/entries_list.dart @@ -47,14 +47,23 @@ class _EntriesListViewState extends State { sort: sort, ); + // Check BuildContext + if (!mounted) return; + final isLastPage = newPage.pagination.currentPage == newPage.pagination.maxPage; + // Prevent duplicates + final currentItemIds = + _pagingController.itemList?.map((e) => e.entryId) ?? []; + final newItems = newPage.items + .where((e) => !currentItemIds.contains(e.entryId)) + .toList(); if (isLastPage) { - _pagingController.appendLastPage(newPage.items); + _pagingController.appendLastPage(newItems); } else { final nextPageKey = pageKey + 1; - _pagingController.appendPage(newPage.items, nextPageKey); + _pagingController.appendPage(newItems, nextPageKey); } } catch (error) { _pagingController.error = error; diff --git a/lib/src/screens/entries/entry_page.dart b/lib/src/screens/entries/entry_page.dart index 20dde59..0c4d05d 100644 --- a/lib/src/screens/entries/entry_page.dart +++ b/lib/src/screens/entries/entry_page.dart @@ -60,14 +60,23 @@ class _EntryPageState extends State { sort: commentsSort, ); + // Check BuildContext + if (!mounted) return; + final isLastPage = newPage.pagination.currentPage == newPage.pagination.maxPage; + // Prevent duplicates + final currentItemIds = + _pagingController.itemList?.map((e) => e.commentId) ?? []; + final newItems = newPage.items + .where((e) => !currentItemIds.contains(e.commentId)) + .toList(); if (isLastPage) { - _pagingController.appendLastPage(newPage.items); + _pagingController.appendLastPage(newItems); } else { final nextPageKey = pageKey + 1; - _pagingController.appendPage(newPage.items, nextPageKey); + _pagingController.appendPage(newItems, nextPageKey); } } catch (error) { _pagingController.error = error; @@ -202,8 +211,7 @@ class _EntryPageState extends State { setState(() { _pagingController.itemList = newList; }); - }, - opUserId: widget.initData.user.userId), + }, opUserId: widget.initData.user.userId), ), ), ) @@ -212,4 +220,10 @@ class _EntryPageState extends State { ), ); } + + @override + void dispose() { + _pagingController.dispose(); + super.dispose(); + } } diff --git a/lib/src/screens/explore/domains_screen.dart b/lib/src/screens/explore/domains_screen.dart index cec18c5..154cea0 100644 --- a/lib/src/screens/explore/domains_screen.dart +++ b/lib/src/screens/explore/domains_screen.dart @@ -42,14 +42,23 @@ class _DomainsScreenState extends State { search: search.isEmpty ? null : search, ); + // Check BuildContext + if (!mounted) return; + final isLastPage = newPage.pagination.currentPage == newPage.pagination.maxPage; + // Prevent duplicates + final currentItemIds = + _pagingController.itemList?.map((e) => e.domainId) ?? []; + final newItems = newPage.items + .where((e) => !currentItemIds.contains(e.domainId)) + .toList(); if (isLastPage) { - _pagingController.appendLastPage(newPage.items); + _pagingController.appendLastPage(newItems); } else { final nextPageKey = pageKey + 1; - _pagingController.appendPage(newPage.items, nextPageKey); + _pagingController.appendPage(newItems, nextPageKey); } } catch (error) { _pagingController.error = error; diff --git a/lib/src/screens/explore/magazines_screen.dart b/lib/src/screens/explore/magazines_screen.dart index fabd179..d81e159 100644 --- a/lib/src/screens/explore/magazines_screen.dart +++ b/lib/src/screens/explore/magazines_screen.dart @@ -45,14 +45,23 @@ class _MagazinesScreenState extends State { search: search.isEmpty ? null : search, ); + // Check BuildContext + if (!mounted) return; + final isLastPage = newPage.pagination.currentPage == newPage.pagination.maxPage; + // Prevent duplicates + final currentItemIds = + _pagingController.itemList?.map((e) => e.magazineId) ?? []; + final newItems = newPage.items + .where((e) => !currentItemIds.contains(e.magazineId)) + .toList(); if (isLastPage) { - _pagingController.appendLastPage(newPage.items); + _pagingController.appendLastPage(newItems); } else { final nextPageKey = pageKey + 1; - _pagingController.appendPage(newPage.items, nextPageKey); + _pagingController.appendPage(newItems, nextPageKey); } } catch (error) { _pagingController.error = error; diff --git a/lib/src/screens/explore/users_screen.dart b/lib/src/screens/explore/users_screen.dart index 9dfd2f5..43a50e1 100644 --- a/lib/src/screens/explore/users_screen.dart +++ b/lib/src/screens/explore/users_screen.dart @@ -41,14 +41,23 @@ class _UsersScreenState extends State { filter: filter, ); + // Check BuildContext + if (!mounted) return; + final isLastPage = newPage.pagination.currentPage == newPage.pagination.maxPage; + // Prevent duplicates + final currentItemIds = + _pagingController.itemList?.map((e) => e.userId) ?? []; + final newItems = newPage.items + .where((e) => !currentItemIds.contains(e.userId)) + .toList(); if (isLastPage) { - _pagingController.appendLastPage(newPage.items); + _pagingController.appendLastPage(newItems); } else { final nextPageKey = pageKey + 1; - _pagingController.appendPage(newPage.items, nextPageKey); + _pagingController.appendPage(newItems, nextPageKey); } } catch (error) { _pagingController.error = error; diff --git a/lib/src/screens/posts/post_page.dart b/lib/src/screens/posts/post_page.dart index bff6aad..ce6d003 100644 --- a/lib/src/screens/posts/post_page.dart +++ b/lib/src/screens/posts/post_page.dart @@ -60,14 +60,23 @@ class _PostPageState extends State { sort: commentsSort, ); + // Check BuildContext + if (!mounted) return; + final isLastPage = newPage.pagination.currentPage == newPage.pagination.maxPage; + // Prevent duplicates + final currentItemIds = + _pagingController.itemList?.map((e) => e.commentId) ?? []; + final newItems = newPage.items + .where((e) => !currentItemIds.contains(e.commentId)) + .toList(); if (isLastPage) { - _pagingController.appendLastPage(newPage.items); + _pagingController.appendLastPage(newItems); } else { final nextPageKey = pageKey + 1; - _pagingController.appendPage(newPage.items, nextPageKey); + _pagingController.appendPage(newItems, nextPageKey); } } catch (error) { _pagingController.error = error; @@ -198,8 +207,7 @@ class _PostPageState extends State { setState(() { _pagingController.itemList = newList; }); - }, - opUserId: widget.initData.user.userId), + }, opUserId: widget.initData.user.userId), ), ), ) @@ -208,4 +216,10 @@ class _PostPageState extends State { ), ); } + + @override + void dispose() { + _pagingController.dispose(); + super.dispose(); + } } diff --git a/lib/src/screens/posts/posts_list.dart b/lib/src/screens/posts/posts_list.dart index d324de8..4e495f8 100644 --- a/lib/src/screens/posts/posts_list.dart +++ b/lib/src/screens/posts/posts_list.dart @@ -47,14 +47,23 @@ class _PostsListViewState extends State { sort: sort, ); + // Check BuildContext + if (!mounted) return; + final isLastPage = newPage.pagination.currentPage == newPage.pagination.maxPage; + // Prevent duplicates + final currentItemIds = + _pagingController.itemList?.map((e) => e.postId) ?? []; + final newItems = newPage.items + .where((e) => !currentItemIds.contains(e.postId)) + .toList(); if (isLastPage) { - _pagingController.appendLastPage(newPage.items); + _pagingController.appendLastPage(newItems); } else { final nextPageKey = pageKey + 1; - _pagingController.appendPage(newPage.items, nextPageKey); + _pagingController.appendPage(newItems, nextPageKey); } } catch (error) { _pagingController.error = error; diff --git a/lib/src/screens/profile/notification_screen.dart b/lib/src/screens/profile/notification_screen.dart index a70d7c4..1af09a1 100644 --- a/lib/src/screens/profile/notification_screen.dart +++ b/lib/src/screens/profile/notification_screen.dart @@ -37,14 +37,23 @@ class _NotificationsScreenState extends State { filter: filter, ); + // Check BuildContext + if (!mounted) return; + final isLastPage = newPage.pagination.currentPage == newPage.pagination.maxPage; + // Prevent duplicates + final currentItemIds = + _pagingController.itemList?.map((e) => e.notificationId) ?? []; + final newItems = newPage.items + .where((e) => !currentItemIds.contains(e.notificationId)) + .toList(); if (isLastPage) { - _pagingController.appendLastPage(newPage.items); + _pagingController.appendLastPage(newItems); } else { final nextPageKey = pageKey + 1; - _pagingController.appendPage(newPage.items, nextPageKey); + _pagingController.appendPage(newItems, nextPageKey); } } catch (error) { _pagingController.error = error;