如何停止 Dart 的 .forEach()? [英] How to stop Dart's .forEach()?

查看:26
本文介绍了如何停止 Dart 的 .forEach()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

List data = [1, 2, 3];
data.forEach((value) {
  if (value == 2) {
    // how to stop?
  }
  print(value);
});

我试过 return false; 它在 jQuery 中有效,但它在 Dart 中不起作用.有办法吗?

I tried return false; which works in jQuery, but it does not work in Dart. Is there a way to do it?

推荐答案

您也可以使用 for/in,它隐式地使用了另一个答案中恰当演示的迭代器:

You can also use a for/in, which implicitly uses the iterator aptly demonstrated in the other answer:

List data = [1,2,3];

for(final i in data){
  print('$i');
  if (i == 2){
    break;
  }
}

这篇关于如何停止 Dart 的 .forEach()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆