🍉 加载中...


Dart 语法:sync*、async、async*、yield、yield*、await 笔记

1 minute read
类型 关键字 返回类型 搭档
多元素同步 sync* Iterable<T> yield、yield*
多元素异步 async* stream<T> yield、yield*、await
单元素同步 async Future<T> await

yield、yield* 实例

 1void main() {
 2    getEmojiWithTime(10_.forEach(print);
 3}
 4
 5Iterable<String> getEmojiWithTime(int count) sync* {
 6    yield* getEmoji(count).map((e) => '$e -- Demo');
 7}
 8
 9Iterable<String> getEmoji(int count) sync* {
10    Runes first = Runes('\u{1f47f}');
11    for (int i = 0; i < count; i++) {
12        yield String.fromCharCodes(first.map((e) => e + i));
13    }
14}

注意事项

关于 yield*,其在 sync* 中使用时,后面的表达式应是一个 Iterable<T> 对象。在 async* 中使用时,后面的表达式应为一个 stream<T> 对象。

如有错误欢迎指正,不胜感激。