如何根据某些条件从dart发生器中断开?
fn sync*(...){ yield 1; yield 2; if(someCondition()) //cancel/break the generator yield 3; if(someCondition2()) //cancel/break the generator yield 4; if(someCondition4()) //cancel/break the generator yield 5; }
5jvtdoz21#
当您希望生成器停止生成新值时,只需从生成器中调用return即可:
return
Iterable<int> fn(bool flag) sync* { yield 1; yield 2; if (flag) { return; } yield 3; } void main() { print(fn(true).toList()); // Prints: [1, 2] print(fn(false).toList()); // Prints: [1, 2, 3] }
1条答案
按热度按时间5jvtdoz21#
当您希望生成器停止生成新值时,只需从生成器中调用
return
即可: