The issue I am currently facing is when the screen is first opened the pagination works fine but when I go to another page and comeback to the page the count of the page is again processed and duplicate data is being shown.
Additionally on further checking the code it was found that the below line of code is running in loop :
if (_sc.position.pixels == _sc.position.maxScrollExtent)
And the print statement is called twice(please check below code)
If anyone can please help me resolve this issue.
Let me know if you require any further information from my end.
List.dart
@override
void initState() {
print('current page value init: ${currentPage}');
checkId = Provider.of<LoginProvider>(context, listen: false).id;
categoryCheck = Provider.of<LoginProvider>(context, listen: false).category;
if (categoryCheck != null) {
if (Provider.of<OpenProjectProvider>(context, listen: false)
.items
.isEmpty) {
myfuture = Provider.of<OpenProjectProvider>(context, listen: false)
.readinboxNotification(checkId, currentPage);
}
} else {
if (Provider.of<OpenProjectProvider>(context, listen: false)
.items
.isEmpty) {
myfuture = Provider.of<OpenProjectProvider>(context, listen: false)
.readinboxNotificationInd(checkId, currentPage);
}
}
super.initState();
//According to me the below line is creating the issue
_sc.addListener(() {
//
print('current page getmoredata: ${currentPage}');
if (_sc.position.pixels == _sc.position.maxScrollExtent) { //this statement is running multiple times
_getMoreData(currentPage++);
}
});
}
Output from Print statement
2 current page getmoredata: 1
67 current page getmoredata: 2
yes that the issues, and you got many option here tbh. but lets make it to the basic. first of all,
first opened the pagination works fine but when I go to another page and comeback to the page
you need to preserve your state with something, maybe AutomaticKeepAliveClientMixin will do the job,
and second inside :
if (_sc.position.pixels == _sc.position.maxScrollExtent) { //this statement is running multiple times
_getMoreData(currentPage++);
}
i think my friend that not the nice way to add more data to ui, at least make more filter to you if statement like this :
if (_sc.position.pixels == _sc.position.maxScrollExtent
//you get the idea
&& thisOldData != newData
)
but how do i know my new and old data state?
make a bridge somewhere in your state. i assume you got data from the api, future, async, promises, whatever it called its not there. you need to freeze that ui so user wont keep scrolling, and that little code keep firing.
so why not using streamBuilder? check this someoneexampleanswer.i just sharing the idea, happy coding