【发布时间】:2018-07-17 10:06:58
【问题描述】:
Flutter 新手,从这里学到了很多。
一般情况下,人们会从 web 服务器请求资源,服务器返回一个对象数组:
[ { "key": "value"}, {"key": "value}...]
我们可以使用 FutureBuilder 轻松处理这种情况。
但是我有一个拥有大量数据的服务器,我必须以这种方式获取资源:
- 使用 api 查询记录计数,例如“/resources/counts”
- 使用 api 查询几条记录,例如“/resource?offset=101&limit=20”得到 20 条记录。
- 当用户向下滚动菜单时,将触发“/resource?offset=121&limit=20”以获取另外 20 条记录。
所以我有一个包含某种修复计数的列表,但资源必须从服务器动态加载。怎么做?
一些代码。
@override
Widget build(BuildContext context) {
// _max == -1, request in progress.
return _max == -1
? new CircularProgressIndicator()
: ListView.builder(
padding: const EdgeInsets.all(16.0),
itemBuilder: (context, i) {
return new FutureBuilder(
future: _getFollowingContracts(),
builder: (context, snapshot) {
if (i.isOdd) return new Divider();
switch (snapshot.connectionState) {
case ConnectionState.none:
case ConnectionState.waiting:
case ConnectionState.active:
return new Text('loading...');
case ConnectionState.done:
if (i.isOdd)
return new Divider(
height: 2.0,
);
final int index = i ~/ 2;
// when user scroll down here we got exception
// because only 20 records is available.
// how to get another 20 records?
return _buildRow(_contracts[index]);
}
},
);
},
itemCount: _max * 2,
);
}
【问题讨论】: