I/flutter (20369): page 1
I/flutter (20369): body [{"page":1,"pages":7,"per_page":"50","total":304},[{"id":"ABW","iso2Code":"AW","name":"Aruba","region":{"id":"LCN","iso2code":"ZJ","value":"Latin America & Caribbean "},"adminregion":{"id":"","iso2code":"","value":""},"incomeLevel":{"id":"HIC","iso2code":"XD","value":"High income"},"lendingType":{"id":"LNX","iso2code":"XX","value":"Not classified"},"capitalCity":"Oranjestad","longitude":"-70.0167","latitude":"12.5167"},{"id":"AFG","iso2Code":"AF","name":"Afghanistan","region":{"id":"SAS","iso2code":"8S","value":"South Asia"},"adminregion":{"id":"SAS","iso2code":"8S","value":"South Asia"},"incomeLevel":{"id":"LIC","iso2code":"XM","value":"Low income"},"lendingType":{"id":"IDX","iso2code":"XI","value":"IDA"},"capitalCity":"Kabul","longitude":"69.1761","latitude":"34.5228"},{"id":"AFR","iso2Code":"A9","name":"Africa","region":{"id":"NA","iso2code":"NA","value":"Aggregates"},"adminregion":{"id":"","iso2code":"","value":""},"incomeLevel":{"id":"NA","iso2code":"NA","value":"Aggregates"},"lendingType":{"id":""
I/flutter (20369): page 2
I/flutter (20369): body [{"page":2,"pages":7,"per_page":"50","total":304},[{"id":"CIV","iso2Code":"CI","name":"Cote d'Ivoire","region":{"id":"SSF","iso2code":"ZG","value":"Sub-Saharan Africa "},"adminregion":{"id":"SSA","iso2code":"ZF","value":"Sub-Saharan Africa (excluding high income)"},"incomeLevel":{"id":"LMC","iso2code":"XN","value":"Lower middle income"},"lendingType":{"id":"IDX","iso2code":"XI","value":"IDA"},"capitalCity":"Yamoussoukro","longitude":"-4.0305","latitude":"5.332"},{"id":"CLA","iso2Code":"C6","name":"Latin America and the Caribbean (IFC classification)","region":{"id":"NA","iso2code":"NA","value":"Aggregates"},"adminregion":{"id":"","iso2code":"","value":""},"incomeLevel":{"id":"NA","iso2code":"NA","value":"Aggregates"},"lendingType":{"id":"","iso2code":"","value":"Aggregates"},"capitalCity":"","longitude":"","latitude":""},{"id":"CME","iso2Code":"C7","name":"Middle East and North Africa (IFC classification)","region":{"id":"NA","iso2code":"NA","value":"Aggregates"},"adminregion":{"id":"","iso2code":"","va
I use ScrollController in Pagination ::
ScrollController? scrollController;
inside initState initialize it and pass the scrollListener function::
scrollController = ScrollController()..addListener(_scrollListener);
here is _scrollListener ::
- when extentAfter becomes 0 which means reached the bottom of last item you
call API again but you should also check if last page reached so that you
do not call API again when you reach last page , it is hashed inside the
condition
void _scrollListener() {
debugPrint("extentAfter ::::: " +
scrollController!.position.extentAfter.toString());
if (scrollController!.position.extentAfter <= 0 /*&&
viewModel.pageNumber < viewModel.totalCount + 1*/) {
if (!viewModel.lazyLoading) {
viewModel.lazyLoading = true; //show a loading indicator
// call APi again and inside getNewProducts "${pageNumber++}"
viewModel.getNewProducts(viewModel.catId ?? "");
}
}
}
then you pass scrollController to the listView/GridView controller ::
controller: scrollController,
6条答案
按热度按时间5fjcxozz1#
编辑将sendPagesDataRequest改为以下应该可以
如果你给我json字符串是正确的
编辑
新sendPagesDataRequest的完整代码
编辑
你需要改变
sendPagesDataRequest
,我使用静态字符串假设你的json字符串是这样的
编辑工作demo
编辑完整代码
您可以使用包https://pub.dev/packages/flutter_paginator
它将自动调用您的
REST
与page
参数在下面的演示中,我添加了打印消息,所以你可以看到它在向下滚动时自动调用rest with page
你可以复制粘贴下面运行完整的代码
代码片段
工作演示
完整演示代码
输出
qqrboqgw2#
无限滚动分页是一项坚韧的任务。
除了延迟获取新项目之外,您还希望让用户了解您的当前状态。例如,如果您正在加载第一个页面,您可能希望在屏幕中间显示进度指示器。但是,如果您正在加载后续页面,您可能希望在底部显示进度指示器。错误指示器也是如此。
如果来自服务器的列表为空或已完成,您还需要停止请求新页面,更不用说您可能希望为失败的请求添加“重试”按钮。
现在有一个名为Infinite Scroll Pagination的包可以为您处理所有事情,用法非常简单。为了展示这一点,我将使用来自@chunhunghan answer的相同国家列表示例:
在上面的代码中,我在开头列出的所有问题(以及其他问题)都得到了解决,如果需要,您可以自定义所有内容。
tzdcorbm3#
我已经创建了一个轻量级的无限加载列表与分页的例子。新的项目被要求,因为你到达列表的底部。用法如下:
我们的想法是根据最后一次加载的项目
lastLoaded
而不是页码进行分页。这样做有助于确保在加载页面X之后,如果页面X+1的内容发生更改(即从数据库中添加或删除某些内容),您不会错过或重复任何内容。如果你的API不支持,或者你不想要它,你可以为你的每个项目添加一个页码属性,然后做:
InfiniteList的实现看起来像这样:
完整的要点在这里:https://gist.github.com/tombailey/988f788493cec9b95e7e9e007b8a7a0d
z9smfwbn4#
屏幕_电影_列表.dart
rest_API_helper.dart
API响应
本例基于请求参数如current_page、item_per_page和响应参数如current_page、total_pages、total_items的API
dpiehjr45#
bwitn5fc6#
用户infinite_scroll_pagination的Flutter包。
此软件包包含ListView、SliverList和SliverGrid无限滚动选项,并带有刷新指示符。
ListView无限滚动的示例是here。