Introduction
In this blog, we will look into the Dart Generators. We will first look at what generators are; then, we will discuss two different types of generators: Synchronous generators and Asynchronous generators, with examples. If you are new to Dart and are yet to set up your environment for the language, you can check out this article.
Dart Generators
Dart Generators allow the user to generate a sequence of values. Values can be generated using one of these two generator functions:
- Synchronous Generator
- Asynchronous Generator
Synchronous Generator
The iterable object returned by the Synchronous generator is a collection of values, that may be accessed sequentially. Mark the function body as sync* and use yield statements to supply value to create a Synchronous generator function. For example,
// Synchronous Generator
// this function gives all the non-zero multiple of a particular number up to a certain limit
Iterable<int> multiplesOf(int limit, int factor) sync* {
int num = 1; // Iterator
while (num <= limit) {
// check if the num is within limits or not
if (num % factor == 0) {
// if the current number is a multiple of the factor, yield the number
yield num;
}
num++;
}
}
void main() {
// getting all the multiples of 3 upto 15
Iterable<int> res = multiplesOf(15, 3);
// print all the elements
res.forEach((element) {
print(element);
});
}
Output:
3
6
9
12
15
Explanation:
The above example depicts the working of a Synchronous generator. Here the function multiplesOf takes two parameters: limit and factor. It returns an object of type Iterable<int> type. The returned object contains all the non-zero multiple of the factor variable less than the limit variable. The function is marked as sync*.
First, the variable num is initialized with a value of one. It has been initialized with one instead of zero, as we are only considering non-zero multiples. Then we are using a while loop to iterate over all the numbers from one to the limit. In each iteration, it checks if the current number is a multiple of the factor. If yes, it yields the number. After every iteration, the value of the num is incremented by one.
Asynchronous Generator
A stream object is returned by the Asynchronous generator. A Stream is a method of receiving a series of events. It usually returns a data event, but in case something goes wrong, it returns an error event. Mark the function body as async* and use yield statements to supply value to create an Asynchronous generator function. For example,
// ASynchronous Generator
// this function gives all the non-zero multiple of a particular number up to a certain limit
Stream<int> multiplesOf(int limit, int factor) async* {
int num = 1; // Iterator
while (num <= limit) {
// check if the num is within limits or not
if (num % factor == 0) {
// if the current number is a multiple of the factor, yield the number
yield num;
}
num++;
}
}
void main() {
// getting all the multiples of 3 upto 15
multiplesOf(15, 3).forEach((element) {
print(element);
});
}
Output:
3
6
9
12
15
Explanation:
The above example depicts the working of an Asynchronous generator. It returns an object of type Stream<int> type. The returned object contains all the non-zero multiple of the factor variable less than the limit variable. The structure and working of the multiplesOf function are the same as it was in the previous example, with a minor difference that the function is marked as async* in this case.




