-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path17_async_await.dart
32 lines (29 loc) · 1 KB
/
17_async_await.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
void main() async{
// Future<ProcessData> createData((){ ---> Using Future method --> Not that readable
// return _LoadIdFromDisk.then((id){
// return _fetchNetworkData(id);
// }).then((data){
// return ProcessData(data);
// })
// })
ProcessData processedData = await createData();
print("Processed Data: ${processedData.data}");
}
Future<ProcessData> createData() async{
final id = await _loadIdFromDisk();
final data = await _fetchNetworkData(id);
return ProcessData(data);
}
Future<int> _loadIdFromDisk() async {
await Future.delayed(Duration(seconds: 1)); // Simulating delay
return 42; // Example ID
}//func to load id from disk
// Simulates fetching data from a network using an ID
Future<String> _fetchNetworkData(int id) async {
await Future.delayed(Duration(seconds: 2)); // Simulating network delay
return "Network data for ID $id"; // Example fetched data
}
class ProcessData{
final String data;
ProcessData(this.data);
}