Dart Generators

In this tutorial you will learn about the Dart Generators and its application with practical example.

Dart Generators

Generator is function allows you to lazily produce a sequence of values. Dart comes with built-in support for two type of generator functions –

Table Of Contents

Synchronous generator:- It returns an Iterable object, which delivers values synchronously. A synchronous generator function can be implemented by marking the function body as sync* along with a yield statements to generate values.

Example:-

Output:-

dart_synchronous_generator_example

Asynchronous generator:- It returns a Stream object, which delivers values asynchronously. An asynchronous generator function can be implemented by marking the function body as async*, and use yield statements to generate values.

Example:-

Output:-

dart_asynchronous_generator_example

In this tutorial we have learn about the Dart Generators and its application with practical example. I hope you will like this tutorial.