강의

멘토링

커뮤니티

Inflearn Community Q&A

primg679254's profile image
primg679254

asked

[Code Factory] [Introduction] Completely master the Dart language in 4 hours

4강 마지막 yield* -> yield

Written on

·

252

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);

 

flutter함수형-프로그래밍객체지향

Answer 1

0

codefactory님의 프로필 이미지
codefactory
Instructor

안녕하세요!

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

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

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

감사합니다!

primg679254's profile image
primg679254

asked

Ask a question