• 카테고리

    질문 & 답변
  • 세부 분야

    프로그래밍 언어

  • 해결 여부

    미해결

4강 마지막 yield* -> yield

24.01.30 03:19 작성 조회수 101

0

4강 마지막 끝날 때요

//stream 사용 위해 패키지 불러와서 써야 함
import 'dart:async';

void main() {
  playAllStream().listen((val){
    print(val);
  });
}

Stream<int> playAllStream() async* {
  //yield* 뒤에 따라오는 stream의 값이 다 return 될 때까지 기다린다.
  yield* calculate(1);
  yield* calculate(1000);
}

Stream<int> calculate(int number) async* {
  for(int i=0; i<5; i++){
    yield i * number;
    
    //async* 로 await를 쓸 수 있다.
    await Future.delayed(Duration(seconds: 1));
  }
}

 

playAllStream() 여기 안에 yield* 를 yield로 바꾸면 결과가

0

0

1

1000

2

2000

이렇게 나올거라고 하셨거든요.

이론적으로 이해는 됐는데, 코드 실행이 안되어서요.

어디서 문제인지 모르겠어요.

에러는 아래와 같습니다.

Error: A value of type 'Stream<int>' can't be assigned to a variable of type 'int'. - 'Stream' is from 'dart:async'. yield calculate(1);

 

답변 1

답변을 작성해보세요.

0

안녕하세요!

에러 메세지를 잘 읽어보고 분석 해보세요!

Stream<int> 타입이 int 타입에 할당될 수 없다고 돼있습니다.

yield*이 무엇을 반환하는지 생각해보시면 됩니다.

감사합니다!