一次从一个项目请求数据?

时间:2018-04-10 06:09:12

标签: dart flutter

我正在尝试从提供分页响应的REST端点获取数据。在按钮上单击flutter,我想获得响应中的下一个项目。我想想我想使用一个Stream来抽象出请求的分页性质,并自动轮询下一个结果。

像这样的dartish伪代码:

:

我觉得可能Stream<String> nextUrl(int lastPage=0) { // Get paginated response batch = server.getResponse(lastPage) for (item in batch) { yield item; } // Recurse automatically // Not a problem if the stream is suspended // after every item request. A big problem // if the stream never pauses. await for (String item in nextUrl(lastPage++) { yield item; } } // In flutter class WidgetState extends state<MyPage> { Stream<String> urlStream = new nextUrl(0); String currentItem; ... @override Widget build(BuildContext context) { return new InkWell( onTap: () { (() async { // This is the call I haven't figured out. await item = urlStream().getNext(); setState(() { currentItem = item; }); })(); } ); } } 之类的东西不存在?我应该暂停/取消暂停此流?但是我不确定那样一次只返回一个项目。

1 个答案:

答案 0 :(得分:1)

async包提供允许执行此操作的StreamQueue

var events = new StreamQueue<String>(yourStream);
var first = await events.next;
while (...) {
  ...
  first = await events.next;
}