Last active
March 6, 2021 09:44
-
-
Save madmag77/940e9fa7a39a816e67169a4f9ebf2887 to your computer and use it in GitHub Desktop.
[Dart article] Exceptions in streams 2
This file contains 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(List<String> arguments) async { | |
Stream<int> stream; | |
runZonedGuarded(() { | |
stream = getStream(); | |
}, (e, s) { | |
print('PRODUCER ZONE Exception handled: $e, $s'); | |
}); | |
runZonedGuarded(() { | |
startListener(stream); | |
}, (e, s) { | |
print('CONSUMER ZONE Exception handled: $e, $s'); | |
}); | |
} | |
Stream<int> getStream() { | |
final intsController = StreamController<int>.broadcast(); | |
int i = 42; | |
Timer.periodic(Duration(seconds: 1), (timer) { | |
i += 1; | |
if (i == 46) throw '[Stream] Bad thing happens!'; | |
intsController.add(i); | |
}); | |
return intsController.stream; | |
} | |
void startListener(Stream<int> ints) { | |
ints.listen((event) { | |
print(event); | |
if (event == 44) { | |
throw '[Listener] Bad thing happens!'; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment