Advertisement
Guest User

Untitled

a guest
Sep 30th, 2021
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 1.18 KB | None | 0 0
  1. int idx = 0;
  2. Future<int> getValueExceptionOnEnd() async {
  3.   if (idx < 4) {
  4.     return idx++;
  5.   } else {
  6.     throw RangeError("no more ints for you!");
  7.   }
  8. }
  9.  
  10. Stream<int> onlyWorksOnDesktop() async* {
  11.   try {
  12.     while (true) {
  13.       yield await getValueExceptionOnEnd();
  14.     }
  15.   } on RangeError {
  16.     print("all done! dart should close the stream after this.");
  17.     return;
  18.   }
  19. }
  20.  
  21. Future<int?> _allPlatformsHelper() async {
  22.   try {
  23.     return await getValueExceptionOnEnd();
  24.   } on RangeError {
  25.     return null;
  26.   }
  27. }
  28.  
  29. Stream<int> worksOnAllPlatforms() async* {
  30.   while (true) {
  31.     final value = await _allPlatformsHelper();
  32.     if (value == null) break;
  33.     yield value;
  34.   }
  35.   print("all done! dart should close the stream after this.");
  36. }
  37.  
  38. void bugDemo() async {
  39.   idx = 0;
  40.   await for (var value in worksOnAllPlatforms()) {
  41.     print(value);
  42.   }
  43.   print("worksOnAllPlatforms()'s stream closed.");
  44.  
  45.   idx = 0;
  46.   await for (var value in onlyWorksOnDesktop()) {
  47.     print(value);
  48.   }
  49.   //This never runs when developing for the web with flutter
  50.   //(though, interestingly, it does work with dart2js.)
  51.   print("onlyWorksOnDesktop()'s stream closed.");
  52. }
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement