pagination work for main page

This commit is contained in:
Valentin CZERYBA 2025-05-23 00:01:48 +02:00
parent 510e366216
commit 1bcc8372c6

View File

@ -153,21 +153,10 @@ class _MyHomePageState extends State<ListItemMenu> {
});
}
void _decrementFetchCount() {
setState(() {
if (_fetchCount > 0 && filteredPosts.isNotEmpty) {
_fetchCount--;
}
});
}
void _scrollListener() {
if (_scrollController.position.pixels ==
_scrollController.position.maxScrollExtent) {
_incrementFetchCount();
} else if (_scrollController.position.pixels ==
_scrollController.position.minScrollExtent) {
_decrementFetchCount();
}
_fetchData();
}
@ -475,13 +464,19 @@ class _MyHomePageState extends State<ListItemMenu> {
if (response.statusCode == 200) {
final List<dynamic> body = json.decode(utf8.decode(response.bodyBytes));
print("results fetch : ${body}");
print("fetch count : ${_fetchCount}");
// Update state after getting the response
setState(() {
if (body.isNotEmpty) {
if ((body.isNotEmpty) || (_fetchCount > 0)) {
if (body.isEmpty) {
_fetchCount = 0;
}
// If we have results, map them to Events
filteredPosts = body
.map((e) => Events.fromJson(e as Map<String, dynamic>))
.toList();
filteredPosts.addAll(
body
.map((e) => Events.fromJson(e as Map<String, dynamic>))
.toList(),
);
} else {
// If no results, clear filteredPosts
filteredPosts.clear();