What is Yield Keyword In Flutter ?

· 3 min read
What is 'Yield' Keyword In Flutter
What is 'Yield' Keyword In Flutter

The yield statement can be used only in the generator’s 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 generator’s functions. The generator’s 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. It’s like a return but doesn’t 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 doesn’t 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<dynamic>
Stream<int> 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 Stream<dynamic>
    Stream<int> countDownFrom(int n) async* {
      if (n > 0) {
        yield n;
        yield* countDownFrom(n - 1);
      }
    }
  • sync* yield will return an Iterable<dynamic>
    Iterable<int> genIterates(int max) sync* {
      var i = 0;
      while (i < max) {
        yield i;
        i++;
      }
    }
  • 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);
      }
    }

 

Conclusion:

In this article, We have been through What is ‘Yield’ Keyword In Flutter?

Keep Learning!!! Keep Fluttering !!!

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

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

FlutterAgency.com is our portal Platform dedicated to Flutter Technology and Flutter Developers. The portal is full of cool resources from Flutter like Flutter Widget GuideFlutter ProjectsCode libs and etc.

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

Leave a Reply