Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does 'yield' keyword do in flutter?

What does the yield keyword actually do in Dart?

like image 880
Newaj Avatar asked Apr 20 '19 17:04

Newaj


People also ask

What is yield and yield in flutter?

Short answeryield returns values from an Iterable or a Stream. yield* is used to recursively call its Iterable or Stream function.

What does the yield keyword do?

What does the yield keyword do? Yield is a keyword in Python that is used to return from a function without destroying the states of its local variable and when the function is called, the execution starts from the last yield statement. Any function that contains a yield keyword is termed a generator.

What is sync and async in flutter?

synchronous operation: A synchronous operation blocks other operations from executing until it completes. synchronous function: A synchronous function only performs synchronous operations. asynchronous operation: Once initiated, an asynchronous operation allows other operations to execute before it completes.

What is sync & async in Dart?

Asynchronous (return a Stream of values) Synchronous (return an Iterable with values)


2 Answers

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

like image 72
Jacob Phillips Avatar answered Sep 19 '22 09:09

Jacob Phillips


The accepted answer's link is broken, here is an official link about async* sync* yield* yield.

If you have some experiences with other languages, you might stuck at these keywords. Here are some tips for getting over keywords.

  1. async* sync* yield* yield are called generator functions. You might use these mostly in Bloc pattern.

  2. async* is also a async, you could use Asynchronous as usual.

  3. sync* cannot be used as sync, you will receive the error that noticed "The modifier sync must be followed by a star".

  4. yield and yield* can only be used with generator functions (async* sync*).

And there are four combinations.

  1. async* yield will return a Stream<dynamic>.
Stream<int> runToMax(int n) async* {   int i = 0;   while (i < n) {     yield i;     i++;     await Future.delayed(Duration(seconds: 300));   } } 
  1. async* yield* will call a function and return Stream<dynamic>.
Stream<int> countDownFrom(int n) async* {   if (n > 0) {     yield n;     yield* countDownFrom(n - 1);   } } 
  1. sync* yield will return a Iterable<dynamic>.
Iterable<int> genIterates(int max) sync* {   var i = 0;   while (i < max) {     yield i;     i++;   } } 
  1. sync* yield* will call a function and return Iterable<dynamic>.
Iterable<int> countDownFrom(int n) sync* {   if (n > 0) {     yield n;     yield* countDownFrom(n - 1);   } } 

If there are any errors, please leave a comment to correct the answer.

like image 26
Tokenyet Avatar answered Sep 18 '22 09:09

Tokenyet