Yield keyword in dart
Hello friends
Today we are learning about yield keyword in dart . I know this topic sounds complicated but its quite easy . i won’t use programming or complicated words i will tell you in simple terms .I just need your attention for 2 minutes.
so here we start:
Why we need yield ?
So if we see the return keyword it can only be used once in a function, and it always terminates the function's execution.
As we know we can not return any value inside from the loop.
As you can see in this example
example:-
Yield:-
Get Mohammad Wadho’s stories in your inbox
Join Medium for free to get updates from this writer.
yield is used in generator functions to produce a sequence of values lazily.
When yield is encountered in a generator function, it emits a value to the sequence being generated and pauses the function's execution. The function retains its state, allowing it to resume execution from where it left off the next time a value is requested from the generator.
Yield commonly works with sync* (Synchronous generators) and async* (Asynchronous generators)
Here is the example with sync*
sync* will execute whole program in one go. it will not wait for execution of any statement. And it will return Iterable of int. Or simple words i can say list of int or sequences of values. And we use that list in loop.
async* (Asynchronous generators)
In this async* asynchronous generator example. The getNumbers() function returns or produces sequences of integer.
And inside the loop `await` keyword is used to pause execution until the asynchronous operation completes. `in our case, Future delayed`
Overall, the yield keyword in Dart is a powerful tool for working with sequences of values, enabling the creation of both synchronous and asynchronous generators for efficient and flexible data processing.







