π―Deeper into Dart
1. Async/Await and Futures π
Future<String> fetchUserData() {
// Simulate a network request
return Future.delayed(Duration(seconds: 2), () => 'User Data');
}
void getUserData() async {
String userData = await fetchUserData();
print(userData); // Prints: User Data
}2. Streams π
Stream<int> countStream(int max) async* {
for (int i = 1; i <= max; i++) {
yield i; // Pauses execution, returns the value, then continues from here when resumed
await Future.delayed(Duration(seconds: 1));
}
}
void listenToStream() {
countStream(5).listen((int value) {
print(value); // Prints 1, 2, 3, 4, 5 (each on a new line, once per second)
});
}3. Collections (List, Set, Map) ποΈ
4. Error Handling π
5. Custom Classes and OOP π§©
Assignments π
Last updated