Generators in Dart Last Updated : 12 Jul, 2025 Suggest changes Share 10 Likes Like Report 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. Synchronous Generator in DartThe 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 DartExample: Dart // sync* functions return an iterable Iterable geeksForGeeks(int number) sync* { int geek = number; while (geek >= 0) { // Checking for even number if (geek % 2 == 0) { // 'yield' suspends // the function yield geek; } // Decreasing the // variable geek geek--; } } // Main Function void main() { print("------- Geeks For Geeks --------"); print("Dart Synchronous Generator Example For Printing Even Numbers From 10 In Reverse Order:"); // Printing positive even numbers // from 10 in reverse order geeksForGeeks(10).forEach(print); } Output:------- Geeks For Geeks --------Dart Synchronous Generator Example For Printing Even Numbers From 10 In Reverse Order:1086420Asynchronous Generator in DartThe 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 DartExample: Dart // async* function(s) return an stream Stream geeksForGeeks(int number) async* { int geek = 0; // Checking for every // geek less than number while (geek <= number) yield geek++; // Incrementing geek // after printing it } // Main Function void main() { print("-------- Geeks For Geeks -----------"); print("Dart Asynchronous Generator Example For Printing Numbers Less Than 10:",); // Printing numbers less // than or equal to 10 geeksForGeeks(10).forEach(print); } Output:-------- Geeks For Geeks -----------Dart Asynchronous Generator Example For Printing Numbers Less Than 10:012345678910 A aditya_taparia Follow 10 Article Tags : Dart Dart-basics Dart-Classes Explore Dart Tutorial 7 min read BasicsIntroduction to Dart Programming Language 4 min read Dart SDK Installation 4 min read Dart - Comments 2 min read Dart - Variables 5 min read Operators in Dart 11 min read Dart - Standard Input Output 3 min read Data TypesDart - Data Types 8 min read Basics of Numbers in Dart 6 min read Strings in Dart 6 min read Dart - Sets 6 min read Dart Programming - Map 7 min read Queues in Dart 3 min read Data Enumeration in Dart 3 min read Control FlowSwitch Case in Dart 2 min read Dart - Loops 4 min read Dart - Loop Control Statements (Break and Continue) 4 min read Labels in Dart 2 min read Key FunctionsDart - Anonymous Functions 2 min read Dart - main() Function 2 min read Dart - Common Collection Methods 2 min read How to Exit a Dart Application Unconditionally? 2 min read Dart - Getters and Setters 3 min read Dart - Classes And Objects 4 min read Object-Oriented ProgrammingDart - this keyword 2 min read Dart - Static Keyword 3 min read Dart - Super and This keyword 4 min read Dart - Concept of Inheritance 5 min read Instance and class methods in Dart 3 min read Method Overriding in Dart 3 min read Getter and Setter Methods in Dart 2 min read Abstract Classes in Dart 4 min read Dart - Builder Class 4 min read Concept of Callable Classes in Dart 4 min read Interface in Dart 3 min read Dart - extends Vs with Vs implements 4 min read Dart - Date and Time 3 min read Using await async in Dart 4 min read Dart UtilitiesHow to Combine Lists in Dart? 3 min read Dart - Finding Minimum and Maximum Value in a List 5 min read Dart - Splitting of String 1 min read Dart ProgramsDart - Sort a List 2 min read Dart - String toUpperCase() Function with Examples 1 min read Dart - Convert All Characters of a String in Lowercase 1 min read How to Replace a Substring of a String in Dart? 2 min read How to Check String is Empty or Not in Dart (Null Safety)? 1 min read Exception Handling in Dart 3 min read Assert Statements in Dart 3 min read Fallthrough Condition in Dart 3 min read Concept of Isolates in Dart 2 min read Advance ConceptsDart - Collections 7 min read Dart - Basics of Packages 2 min read Dart - String codeUnits Property 1 min read HTML Document Object Model and Dart Programming 3 min read My Profile ${profileImgHtml} My Profile Edit Profile My Courses Join Community Transactions Logout Like