Last active
May 28, 2026 14:42
-
-
Save guiathayde/8d1b446b3365562952550891730a7808 to your computer and use it in GitHub Desktop.
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:math'; | |
| int randomSeconds() { | |
| var random = Random(); | |
| return random.nextInt(10) + 1; | |
| } | |
| Future<String> callWebApi01() async { | |
| print('Start call to Web API 01...'); | |
| final delay = randomSeconds(); | |
| await Future.delayed(Duration(seconds: delay)); | |
| return 'Data 1 returned in $delay seconds'; | |
| } | |
| Future<String> callWebApi02() async { | |
| print('Start call to Web API 02...'); | |
| final delay = randomSeconds(); | |
| await Future.delayed(Duration(seconds: delay)); | |
| return 'Data 2 returned in $delay seconds'; | |
| } | |
| Stream<String> delayedTextStream() async* { | |
| for (int i = 1; i <= 5; i++) { | |
| await Future.delayed(Duration(seconds: randomSeconds())); | |
| yield 'Text $i'; | |
| } | |
| } | |
| Stream<int> allEvenNumbers() async* { | |
| var num = 0; | |
| while (true) { | |
| num += 2; | |
| yield num; | |
| } | |
| } | |
| void main() async { | |
| // var data1 = await callWebApi01(); | |
| // var data2 = await callWebApi02(); | |
| // | |
| // var results = [data1, data2]; | |
| // print(results); | |
| // var future1 = callWebApi01(); | |
| // var future2 = callWebApi02(); | |
| /* | |
| final results = await Future.wait([ | |
| callWebApi01(), | |
| callWebApi02() | |
| ]); | |
| print(results); | |
| */ | |
| /* | |
| final result = await Future.any([ | |
| callWebApi01(), | |
| callWebApi02() | |
| ]); | |
| print(result); | |
| */ | |
| /* | |
| Stream<String> st = delayedTextStream(); | |
| st.listen((value) { | |
| print(value); | |
| }); | |
| */ | |
| await for (var data in delayedTextStream()) { | |
| print('Received $data'); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment