What does 'yield' keyword do in flutter?

后端 未结 3 1540
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-30 12:44

What does the yield keyword actually do in Dart?

3条回答
  •  时光取名叫无心
    2021-01-30 13:27

    yield adds a value to the output stream of the surrounding async* function. It's like return, but doesn't terminate the function.

    See 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).

提交回复
热议问题