Intermediate Dart
  • Welcome to the Course
  • Course Overview
  • Resources
  • Dart Fundamentals-2
    • Section Overview
    • Basic Concepts
      • Class
      • Object/Instance
      • Instance /Field Variables
      • Methods
      • "this" keyword-1
    • Constructor
      • Default Constructor
      • Parameterised Constructor
        • Basic Parameterised Constructor & "this" keyword -2
        • Parameterised Constructor with Syntactic sugar
        • Optional Arguments in Parameterised Constructor
      • Named Constructor
    • Getters & Setters and Encapsulation
      • Access Modifiers and _ symbol
      • Implicit / Default Getters & Setters
      • Custom Getters and Setters
      • Encapsulation
    • Inheritance
      • Inheritance, Types of Inheritance , Terminology
      • Method Overriding
      • @override annotation
      • super Keyword -1
      • super Keyword -2
    • Polymorphism
    • Static Variables and Methods
    • Abstract Class and Abstract Methods
    • Interface
    • Miscellaneous concepts
      • Untitled
      • Object Class
      • Mixin
      • Immutable Instances
      • Cascade notation
  • Dart Fundamentals -3
    • Section Overview
    • Asynchronous Programming
      • Basic Concept of Asynchronous programming
      • async , await , then
      • Exception Handling in asynchronous code
    • Functional Programming
      • Anonymous functions
      • High Level Iterators
    • Advanced Asynchronous Programming
      • Isolates
      • Streams
      • Generators
      • Microtask and Zones
    • Method & Constructor Overloading
Powered by GitBook
On this page
  • Prerequisite: Iteration, Iterator , Iterable
  • Documentation
  • Sync Generators
  • Async Generators

Was this helpful?

  1. Dart Fundamentals -3
  2. Advanced Asynchronous Programming

Generators

PreviousStreamsNextMicrotask and Zones

Last updated 4 years ago

Was this helpful?

Prerequisite: Iteration, Iterator , Iterable

void main(){
  
  
  List<int> myList = [1,2,4,5,7];
  
  
  for(int i in myList){
    
    print(i);
    
    
  }
  
}
void main(){
  
  
  Set<int> mySet = {1,2,4,5,7};
  
  
  for(int i in mySet){
    
    print(i);
    
    
  }
  
}
void main(){
  
  
  // Example with Map 
  
   Map<int,String> myIterable  ={
     
     
     1:"Apple",
     2:"Orange",
     3:"Banana"
   };
  
  
  
  for(var i in myIterable.keys){
    
    
      print(i);
    
  }
  
  
  for(var i in myIterable.values){
    
    
      print(i);
    
  }
  
  
  
}

In simple words , iterable is something you can loop over or a collection of values that can be accessed sequentially

Iterator is something that remember can "where it is " i.e. current position during iteration

Documentation

Sync Generators

There are two types of Generators in Dart

In a normal function you would expect the function body to execute when you call the function , but in case of generator it doesn't execute

void main() {
  print("Start of main");

  Iterable<int> numbers = getNumbers(6);

  print("End of main ");
}

Iterable<int> getNumbers(int number) sync* {
  print("Number Generation Started");

  for (int i = 0; i < 0; i++) {
    yield i;
  }

  print("Number Generation ended");
}

The generator starts executing when you start iterating over iterable it returned

void main() {
  print("Start of main");

  Iterable<int> numbers = getNumbers(6);

  for (var i in numbers) {
    print(i);
  }

  print("End of main ");
}

Iterable<int> getNumbers(int number) sync* {
  print("Number Generation Started");

  for (int i = 0; i < number; i++) {
    yield i;
  }

  print("Number Generation ended");
}

You can think of Generators as a kind of special function . In normal function when you call it , it executes the statements inside it , then returns a value . In case of generators it returns an iterator . When you iterate over the values it runs the statements in the generator and every time it hits "yield" it produces a value ("yield" pauses the execution of the generator and returns a value) So you are able to call generators multiple times and every time it generates a value for you , hence the name . Until you exhaust the generator , you can keep calling it and it will generate a value

Async Generators

Synchronous Generator Returns an iterable

Asynchronous Generator Returns a stream

https://dart.dev/codelabs/iterablesdart.dev
Iterator class - dart:core library - Dart API
Logo