In my project. There is a list view that show data, come from API with pagination.
I used SmartRefresher
for API pagination and inside of it use ListView.builder()
to show data list view. Here is my problem. When I fetch next pagination with SmartRefresher
(Eg: skipCount is 10).Then ListView.builder()
only show new fresh data and old data Map
are disappear. What is wanted and expected is when load new data, the new fresh data will showed next to the old data list.(Like in Social media). How can I do that?
This is my code implementation of SmartRefresher()
.
return SmartRefresher(
enablePullDown: true,
enablePullUp: true,
controller: _refreshController,
header: const WaterDropHeader(),
footer: CustomFooter(
builder: (context, LoadStatus? mode) {
Widget body;
if (mode == LoadStatus.idle) {
body = Row(
mainAxisAlignment: MainAxisAlignment.center,
children: const [
Icon(
Icons.arrow_downward_rounded,
size: 15,
),
],
);
} else if (mode == LoadStatus.loading) {
body = const CircularProgressIndicator();
} else if (mode == LoadStatus.failed) {
body = const Text("Load Failed!Click retry!");
} else if (mode == LoadStatus.canLoading) {
body = Row(
mainAxisSize: MainAxisSize.min,
children: const [
Icon(
Icons.arrow_downward,
color: Colors.grey,
),
SizedBox(
width: 8.0,
),
Text("Load more..."),
],
);
} else {
body = const Text("No more Data");
}
return SizedBox(
height: 55.0,
child: Center(child: body),
);
},
),
onRefresh: () {
widget.notificationBloc.skipCount = 0;
_refreshController.resetNoData();
widget.notificationBloc
.getNotificationList()
.then((value) => _refreshController.refreshCompleted());
},
onLoading: () {
widget.notificationBloc.skipCount += widget.notificationBloc.limitCount;
if (widget.notificationBloc.notificationListModel!.total <=
widget.notificationBloc.skipCount) {
_refreshController.loadNoData();
} else {
widget.notificationBloc
.getNotificationList(
skip: widget.notificationBloc.skipCount,
fetchLimitCount: widget.notificationBloc.limitCount)
.then((value) => _refreshController.loadComplete());
}
},
child: ListView.builder(...),
);
This is my code implementation of fetching Data List with pagination.
final _notificationListController = PublishSubject<MessageState>();
Stream<MessageState> get notificationListStream => _notificationListController.stream;
int skipCount = 0;
int limitCount = 20;
getNotificationList({int skip = 0, int fetchLimitCount = 20}) async {
try {
await _notificationRepository
.getNotificationList(
notificationListEndPoint: END_POINT_NOTIFICATION_LIST,
startingNumber: skip,
fetchCount: fetchLimitCount)
.then(
(responseValue) {
_notificationListController.sink.add(MessageState.loading);
if (responseValue?.messageState == MessageState.data) {
notificationListModel = responseValue!.data;
notificationListData = notificationListModel?.data ?? [];
_notificationListController.sink.add(MessageState.data);
} else if (responseValue?.messageState == MessageState.requestError) {
errorHandlingModel = responseValue?.data;
_notificationListController.sink.add(MessageState.requestError);
} else if (responseValue?.messageState == MessageState.serverError) {
_notificationListController.sink.add(MessageState.serverError);
}
},
);
} catch (e) {
errorMessage = e.toString();
_notificationListController.sink.add(MessageState.exception);
}
}
2
Answers
I found what I need to do is in BLOC logical operation I need to change from
notificationListData = notificationListModel?.data ?? [];
tonotificationListData.addAll(notificationListModel?.data ?? []);
. Because I realize that I want to show new fetching data will add to the old data in same list.I have implement same Dependency but its working fine may be some method or steps you missed. I will Share my Git code please check pagiation and Swipe refresh both implemented using Getx.
Dependency: pull_to_refresh: ^2.0.0 or latest
Example of pagination and SwipeRefresh:
https://github.com/SharmaJatin1997/SwipeRefreshAndPagination
Hope it will helpful for you..