VOOZH about

URL: https://www.geeksforgeeks.org/dart/generators-in-dart/

⇱ Generators in Dart - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Generators in Dart

Last Updated : 12 Jul, 2025

Generators in Dart allow the user to produce a sequence of values easily. One can generate a sequence of values in Dart with the help of two generator functions :

  • Synchronous Generator: Returns an Iterable object.
  • Asynchronous Generator: Returns a Stream object.
👁 generator_in_dart


Synchronous Generator in Dart

The synchronous generator returns an iterable object i.e. it returns the collection of values, or "elements", that can be accessed sequentially. To implement synchronous generator function, mark the function body as sync*, and use yield statements to deliver value(s).

Implementing a synchronous generator in Dart

Example:


Output:

------- Geeks For Geeks --------
Dart Synchronous Generator Example For Printing Even Numbers From 10 In Reverse Order:
10
8
6
4
2
0

Asynchronous Generator in Dart

The asynchronous generator returns a stream object. A Stream provides a way to receive a sequence of events. Each event is either a data event, also called an element of the stream, or an error event, which is a notification that something has failed. To implement an asynchronous generator function, mark the function body as async* and use yield statements to deliver value(s).

Implementing an asynchronous generator in Dart

Example:

Output:

-------- Geeks For Geeks -----------
Dart Asynchronous Generator Example For Printing Numbers Less Than 10:
0
1
2
3
4
5
6
7
8
9
10


Comment
Article Tags:

Explore