Created
          September 3, 2020 15:46 
        
      - 
      
 - 
        
Save graphicbeacon/e9e9db9c339c29d63a32f55b44d6a38d to your computer and use it in GitHub Desktop.  
    Async Generators example
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | import 'dart:async'; | |
| void main() { | |
| // asynchronousNaturalsTo(10).listen(print); | |
| final controller = asyncNaturalsTo(10); | |
| controller.stream.listen((n) => print('YEss $n'), | |
| onDone: () { | |
| controller.close(); | |
| }); | |
| } | |
| Stream<int> asynchronousNaturalsTo(int n) async* { | |
| int k = 0; | |
| while (k < n) yield k++; | |
| } | |
| StreamController<int> asyncNaturalsTo(int n) { | |
| final controller = StreamController<int>(); | |
| int k = 0; | |
| while (k < n) { | |
| controller.add(k); | |
| k++; | |
| } | |
| return controller; | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment