Created
November 20, 2024 11:48
-
-
Save kumar-aakash86/53c529ef49cb95ed35294d4afe3a7179 to your computer and use it in GitHub Desktop.
Dart Async Programming
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
//https://dartpad.dev/?id=53c529ef49cb95ed35294d4afe3a7179 | |
void main() { | |
print('Fetching data...'); | |
// fetchData(); | |
// String data = await fetchDataAsync(); | |
// print(data); | |
// connectStream(); | |
print('Doing other work...'); | |
} | |
void fetchData() { | |
Future.delayed(Duration(seconds: 2), () { | |
print('Data fetched'); | |
}); | |
} | |
Future<String> fetchDataAsync() async { | |
await Future.delayed(Duration(seconds: 2)); | |
return 'Data fetched'; | |
} | |
void connectStream()async { | |
await for (int value in countStream()) { | |
print(value); | |
} | |
} | |
Stream<int> countStream() async* { | |
for (int i = 1; i <= 5; i++) { | |
await Future.delayed(Duration(seconds: 1)); | |
yield i; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment