> For the complete documentation index, see [llms.txt](https://eece.gitbook.io/mist-innovation-club-flutter-course-1/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://eece.gitbook.io/mist-innovation-club-flutter-course-1/dart-fundamentals-3/advanced-asynchronous-programming/generators.md).

# Generators

## Prerequisite: Iteration, Iterator , Iterable

{% embed url="<https://youtu.be/vaaEpCXFc_A>" %}

{% tabs %}
{% tab title="First Tab" %}

```dart
void main(){
  
  
  List<int> myList = [1,2,4,5,7];
  
  
  for(int i in myList){
    
    print(i);
    
    
  }
  
}
```

{% endtab %}

{% tab title="Second Tab" %}

```dart
void main(){
  
  
  Set<int> mySet = {1,2,4,5,7};
  
  
  for(int i in mySet){
    
    print(i);
    
    
  }
  
}
```

{% endtab %}

{% tab title="Third Tab" %}

```dart
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);
    
  }
  
  
  
}
```

{% endtab %}
{% endtabs %}

{% hint style="success" %}
In simple words , iterable is something you can loop over or a collection of values that can be accessed sequentially&#x20;
{% endhint %}

{% hint style="success" %}
Iterator is something that remember can  "where it is " i.e. current position during iteration <br>
{% endhint %}

![](https://1972396024-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MCuqiBdVbVkT07RwbQD%2F-MHaQJKZR5HbaW1iikx4%2F-MHaQV9A2N0gZMLy53Pk%2Fimage.png?alt=media\&token=503cd874-db1b-4602-be7c-c14d98867eba)

### Documentation

{% tabs %}
{% tab title="Iterables " %}
{% embed url="<https://dart.dev/codelabs/iterables>" %}

{% endtab %}

{% tab title="Iterators" %}
{% embed url="<https://api.dart.dev/stable/2.9.3/dart-core/Iterator-class.html>" %}

{% endtab %}
{% endtabs %}

## Sync Generators&#x20;

{% embed url="<https://youtu.be/926ns6bb8U0>" %}

There are two types of Generators in Dart

* Synchronous Generator  <img src="https://1972396024-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MCuqiBdVbVkT07RwbQD%2F-MHXakLQ-7B6ElxZncUA%2F-MHXcaFnds4SP7FRYp2o%2Fright-arrow.png?alt=media&amp;token=fabfa426-8a8c-4418-a71f-247d88258189" alt="" data-size="line"> Returns an iterable
* Asynchronous Generator <img src="https://1972396024-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MCuqiBdVbVkT07RwbQD%2F-MHXakLQ-7B6ElxZncUA%2F-MHXcaFnds4SP7FRYp2o%2Fright-arrow.png?alt=media&amp;token=fabfa426-8a8c-4418-a71f-247d88258189" alt="" data-size="line"> Returns a stream&#x20;

{% tabs %}
{% tab title="First Tab" %}
{% hint style="success" %}
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&#x20;
{% endhint %}

```dart
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");
}

```

{% endtab %}

{% tab title="Second Tab" %}
{% hint style="success" %}
The generator starts executing when you start iterating over iterable it returned
{% endhint %}

```dart
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");
}

```

{% hint style="info" %}
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&#x20;
{% endhint %}
{% endtab %}
{% endtabs %}

## Async Generators&#x20;
