Yield Flutter là gì

The yield statement can be used only in the generators functions. So in this article, We will go through Yield Keyword In Flutter?

What is Yield Keyword In Flutter?

The yield statement can be used only in the generators functions. The generators function generates data items in a natural way as calculated, received from outside, predefined values, etc.

What does the Yield do?

When the next data item is ready then the yield statement sends this item into a data sequence which is essentially the generation result of the function. The data sequence can be synchronous or asynchronous.

In the Dart language, the synchronous data sequence means the instance of Iterable.

The asynchronous data sequence means the instance of Stream.

Yield adds a value to the output stream of the surrounding async* function. Its like a return but doesnt terminate the function.

Consider a code snippet below:

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 doesnt necessarily suspend though in the current implementations it does.

  • async* sync* yield* yield is called generator functions. It is used mostly in the Bloc pattern.
  • async* is also an async, you could use Asynchronous as usual.
  • sync* cannot be used as sync, you will receive the error that noticed The modifier sync must be followed by a star.
  • yield and yield* can only be used with generator functions async* sync*.

And there are four combinations.

  • async* yield will return a Stream
Stream runToMax[int n] async* { int i = 0; while [i < n] { yield i; i++; await Future.delayed[Duration[seconds: 300]]; } }
  • async* yield* will call a function and return StreamStream countDownFrom[int n] async* { if [n > 0] { yield n; yield* countDownFrom[n - 1]; } }
  • sync* yield will return an IterableIterable genIterates[int max] sync* { var i = 0; while [i < max] { yield i; i++; } }
  • sync* yield* will call a function and return IterableIterable countDownFrom[int n] sync* { if [n > 0] { yield n; yield* countDownFrom[n - 1]; } }

Conclusion:

In this article, We have been throughWhat is Yield Keyword In Flutter?

KeepLearning!!! KeepFluttering!!!

Still, need Support for Flutter Development? Do let us know.

Also, check this blog for Persisting Drawer through App in flutter

FlutterAgency.comis our portal Platform dedicated toFlutter TechnologyandFlutter Developers. The portal is full of cool resources from Flutter likeFlutter WidgetGuide,Flutter Projects,Code libsand etc.

FlutterAgency.com is one of the most popular online portals dedicated toFlutter Technology and daily thousands of unique visitors come to this portal to enhance their knowledge ofFlutter.

Share this:

  • Click to share on Twitter [Opens in new window]
  • Click to share on Facebook [Opens in new window]
  • Click to share on LinkedIn [Opens in new window]
  • Click to share on Reddit [Opens in new window]
  • Click to share on Tumblr [Opens in new window]
  • Click to share on WhatsApp [Opens in new window]
Advertisement

Video liên quan

Chủ Đề