■ Stream<T> 클래스에서 async*/yield 키워드를 사용하는 방법을 보여준다.
▶ 예제 코드 (DART)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
import 'dart:async'; void main() async { Stream<String> stream = await getStream(['A', 'B', 'C', 'D', 'E']); int count = await Print(stream); print(count); } const Duration duration = Duration(seconds: 1); Stream<String> getStream(Iterable<String> itemIterable) async* { for (String item in itemIterable) { await Future.delayed(duration); yield item; } } Future<int> Print(Stream<String> stream) async { int count = 0; await for (String item in stream) { print(item); count++; } return count; } |