flutter - 从流一一获得 future

标签 flutter dart

我有一个流从Web api返回值,而一个小部件显示一个值(使用FutureBuilder)。用户可以使用一个简单的下一个按钮迭代这些值。我不想预先加载所有值,但是我不想在按下下一步按钮时加载每个值。

我当前的代码是:

Queue<Item> itemQueue = Queue<Item>();
Future<Item> curItem;

Future<Item> getItem() async {
  while (itemQueue.isEmpty)
    await Future.delayed(Duration(milliseconds: 250));
  return itemQueue.removeFirst();
}

@override
void initState() {
  // ...
  final sub = stream.listen((item) async {
    itemQueue.add(item);
  });
  // ...
}

@override
Widget build(BuildContext context) {
  // ...
  ItemWidget(curItem)
  // ...
  RaisedButton(child: Text("next"),
        onPressed: (){
          setState(() {
            curItem = getItem();
          });
        },)
  // ...
}

这种方法有效,但感觉这不是最正确/最优雅的方法。

有没有更好的办法?

谢谢!

最佳答案

编辑:

正如评论中指出的那样,创建广播流的原始答案可能会导致事件掉在地板上。为了避免创建广播流,我们可以使用StreamIterator一次遍历流中的一个元素:

final myStream = Stream.fromIterable([1,2,3,4,5]);

// We need to be able to listen to the stream multiple times.
final iter = StreamIterator(myStream);

// The iterator doesn't start at the first element, so we need to
// do that ourselves.
while (await iter.moveNext()) {
  // StreamIterator.current will always point to the currently selected
  // element of the stream.
  print(iter.current);
}

原文:
您应该能够使用Stream.asBroadcastStreamStream.take来执行此操作。

您的代码如下所示:

final myStream = Stream.fromIterable([1,2,3,4,5]);

// We need to be able to listen to the stream multiple times.
final stream = myStream.asBroadcastStream();

for (int i = 0; i < 5; ++i) {
  // Creates a new Stream with 1 element which we can call Stream.first on
  // This also changes the contents of stream.
  print(await stream.take(1).first);
}

哪个会输出:
1
2
3
4
5

关于flutter - 从流一一获得 future ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59888242/

相关文章:

flutter onPressed() 与 onTap()

android - 在 Google Play 上使用调试 key 签名

firebase - 如何在 Flutter 上刷新 firebase token ?

flutter - 通过DropdownButton在正确的类别上显示文本字段

flutter - flutter 如何将参数传递到其他屏幕

Flutter DART "Try adding an explicit type like ' 动态',或在您的分析选项文件中启用隐式动态。”

flutter - 为什么我从负数的模数运算中得到不正确的输出

flutter - 如何检查 Dart 中 switch case 的异常子类型?

android - 如何将 'Google Consent ' 添加到 flutter 应用程序

json - 尝试将类型 'FolderBasedDartSdk' 用作函数