首页 > 解决方案 > 键值未来任务队列

问题描述

我想创建一个未来任务队列,并使用一个键来避免将已添加的相同任务添加到队列中。

这是我的场景:

我需要的是:

我阅读了有关 StreamQueue 和 Queue 的信息,但我不知道它是否适合我的需要。

我读到的另一个结构是await for流。

所以我尝试了类似的东西:

await for (var url in stream) { // <--- I do not know how create this stream
  var url = Uri.https('www.example.com', url);

  List<dynamic> tmpItems;

  try {
    http.Response res = await http.get(url);
    final data = json.decode(res.body);
    tmpItems = _parseItems(data["data"]);
  } catch(e) {
    print(e);
  }

  if (this.mounted) {
    return setState(() {
      _items.addAll(tmpItems ?? []);
    });
  }
}

我不知道如何创建stream和添加对它的调用。

标签: asynchronousdartflutterqueue

解决方案


好的,首先你需要有一个 urlArray 有 url 然后实现一个名为 getUrl 的异步方法作为示例并实现它以从 url 获取数据此方法将 url 作为参数

Future getData(url,index) async {
       //here you can call the function and handle the output(return value) as result
       getUrl(url).then((result) {
           // print(result);
           //here check if index is smaller than the array length-1
           //if true then call getData function again to get next url
           if(index<urlArray.length)
           {
             --index;
             getData(urlArray[index],index);
            }
      });
 }

所以第一次调用会是这样的 getData(urlArray[0],0)


推荐阅读