Generators in Dart allows the user to produce a sequence of value easily. One can generate a sequence of values in dart with the help of two generator functions :
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).
Example: Implementing a synchronous generator in Dart.
--------------------- 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).
Example: Implementing an asynchronous generator in Dart.
--------------------- Geeks For Geeks --------------------- Dart Asynchronous Generator Example For Printing Numbers Less Than 10: 0 1 2 3 4 5 6 7 8 9 10
- How to Combine Lists in Dart?
- Basics of Numbers in Dart
- Dart - Super and This keyword
- Dart - Standard Input Output
- Dart - Classes And Objects
- Operators in Dart
- Dart - Loops
- Switch Case in Dart
- Dart - Loop Control Statements (Break and Continue)
- Dart Programming - Functions
- Instance and class methods in Dart
- Abstract Classes in Dart
- Labels in Dart
- Dart Programming - List
- Different Types of Functions in Dart
- Dart - Splitting of String
- Method Overriding in Dart
- Concept of Isolates in Dart
- Concept of Callable Classes in Dart
- Getter and Setter Methods in Dart
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.