'yield' 关键字在 flutter 中有什么作用?
What does 'yield' keyword do in flutter?
yield
关键字在 Dart 中的实际作用是什么?
yield
向周围 async*
函数的输出流添加一个值。类似于return
,但不会终止函数。
见https://dart.dev/guides/language/language-tour#generators
Stream asynchronousNaturalsTo(n) async* {
int k = 0;
while (k < n) yield k++;
}
When the yield statement executes, it adds the result of evaluating its expression to the stream. It doesn’t necessarily suspend (though in the current implementations it does).
yield
语句只能在生成器函数中使用。
生成器的函数以自然方式生成数据项(如计算的、从外部接收的、预定义的值等)。
当下一个数据项准备就绪时,yield
语句将此项发送到数据序列中,这实际上是函数的生成结果。
数据序列可以是同步的也可以是异步的。
在 Dart 语言中,同步数据序列是指 Iterable
.
的实例
异步数据序列是指Stream
的实例。
P.S.
生成器函数可以无限生成数据项,直到函数 returns.
但与普通函数不同的是,函数调用后会立即返回结果(数据序列),可以立即使用。
在这种情况下,只有当生成器函数终止(成功或失败)时才能到达数据序列的末尾。
已接受的答案link已损坏,here是关于async* sync* yield* yield
的官方link。
如果您有使用其他语言的经验,您可能会卡在这些关键字上。这里有一些 提示 来克服关键字。
async* sync* yield* yield
被称为 生成器函数 。您可能主要在 Bloc pattern.
中使用这些
async*
也是一个async
,你可以照常使用异步。
sync*
不能用作 sync
,您将收到注意到 "The modifier sync must be followed by a star".[= 的错误33=]
yield
和 yield*
只能与 生成器函数一起使用 (async*
sync*
) .
并且有四种组合。
async* yield
会 return 一个 Stream<dynamic>
.
Stream<int> runToMax(int n) async* {
int i = 0;
while (i < n) {
yield i;
i++;
await Future.delayed(Duration(seconds: 300));
}
}
async* yield*
将调用一个函数并且 return Stream<dynamic>
.
Stream<int> countDownFrom(int n) async* {
if (n > 0) {
yield n;
yield* countDownFrom(n - 1);
}
}
sync* yield
会 return 一个 Iterable<dynamic>
.
Iterable<int> genIterates(int max) sync* {
var i = 0;
while (i < max) {
yield i;
i++;
}
}
sync* yield*
将调用一个函数并且 return Iterable<dynamic>
.
Iterable<int> countDownFrom(int n) sync* {
if (n > 0) {
yield n;
yield* countDownFrom(n - 1);
}
}
如有错误,欢迎留言指正。
我认为 yield* 的正确答案是委托给另一个生成器而不是调用一个函数。 yield* 简单地委托给另一个生成器,这意味着当前的生成器停止,另一个生成器接管工作直到它停止生产。在那个停止产生值之后,主生成器恢复产生它自己的值。
感谢@András Szepesházi 鼓励我post将此评论作为答案,希望对您有所帮助。
yield
关键字在 Dart 中的实际作用是什么?
yield
向周围 async*
函数的输出流添加一个值。类似于return
,但不会终止函数。
见https://dart.dev/guides/language/language-tour#generators
Stream asynchronousNaturalsTo(n) async* {
int k = 0;
while (k < n) yield k++;
}
When the yield statement executes, it adds the result of evaluating its expression to the stream. It doesn’t necessarily suspend (though in the current implementations it does).
yield
语句只能在生成器函数中使用。
生成器的函数以自然方式生成数据项(如计算的、从外部接收的、预定义的值等)。
当下一个数据项准备就绪时,yield
语句将此项发送到数据序列中,这实际上是函数的生成结果。
数据序列可以是同步的也可以是异步的。
在 Dart 语言中,同步数据序列是指 Iterable
.
的实例
异步数据序列是指Stream
的实例。
P.S.
生成器函数可以无限生成数据项,直到函数 returns.
但与普通函数不同的是,函数调用后会立即返回结果(数据序列),可以立即使用。
在这种情况下,只有当生成器函数终止(成功或失败)时才能到达数据序列的末尾。
已接受的答案link已损坏,here是关于async* sync* yield* yield
的官方link。
如果您有使用其他语言的经验,您可能会卡在这些关键字上。这里有一些 提示 来克服关键字。
async* sync* yield* yield
被称为 生成器函数 。您可能主要在 Bloc pattern. 中使用这些
async*
也是一个async
,你可以照常使用异步。sync*
不能用作sync
,您将收到注意到 "The modifier sync must be followed by a star".[= 的错误33=]yield
和yield*
只能与 生成器函数一起使用 (async*
sync*
) .
并且有四种组合。
async* yield
会 return 一个Stream<dynamic>
.
Stream<int> runToMax(int n) async* {
int i = 0;
while (i < n) {
yield i;
i++;
await Future.delayed(Duration(seconds: 300));
}
}
async* yield*
将调用一个函数并且 returnStream<dynamic>
.
Stream<int> countDownFrom(int n) async* {
if (n > 0) {
yield n;
yield* countDownFrom(n - 1);
}
}
sync* yield
会 return 一个Iterable<dynamic>
.
Iterable<int> genIterates(int max) sync* {
var i = 0;
while (i < max) {
yield i;
i++;
}
}
sync* yield*
将调用一个函数并且 returnIterable<dynamic>
.
Iterable<int> countDownFrom(int n) sync* {
if (n > 0) {
yield n;
yield* countDownFrom(n - 1);
}
}
如有错误,欢迎留言指正。
我认为 yield* 的正确答案是委托给另一个生成器而不是调用一个函数。 yield* 简单地委托给另一个生成器,这意味着当前的生成器停止,另一个生成器接管工作直到它停止生产。在那个停止产生值之后,主生成器恢复产生它自己的值。
感谢@András Szepesházi 鼓励我post将此评论作为答案,希望对您有所帮助。