-
Notifications
You must be signed in to change notification settings - Fork 2
/
loading_isolate.dart
53 lines (47 loc) · 1.38 KB
/
loading_isolate.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import 'dart:convert';
import 'dart:isolate';
import 'package:http/http.dart' as http;
void main() async {
await loadDataFromIsolate();
}
Future<void> loadDataFromIsolate() async {
List<Map<String, dynamic>> result = [];
final receivePort = ReceivePort();
final isolate = await Isolate.spawn(fetchDataIsolate, receivePort.sendPort);
await for (var response in receivePort) {
if (response == null) {
break; // Exit the loop on completion signal
}
if (response is Map<String, dynamic>) {
result.add(response);
}
}
print('Isolate execution completed');
}
void fetchDataIsolate(SendPort sendPort) async {
for (int i = 0; i < 10; i++) {
sendPort.send(await fetchData(requestCount: 5));
}
sendPort.send(null); // Signal completion
}
Future<Map<String, dynamic>> fetchData({int requestCount = 1}) async {
final url = Uri.parse('https://api.example.com/data');
List<Future<http.Response>> futures = [];
for (int j = 0; j < requestCount; j++) {
futures.add(http.get(url));
}
try {
final responses = await Future.wait(futures);
for (var response in responses) {
if (response.statusCode == 200) {
final jsonData = jsonDecode(response.body);
return jsonData;
} else {
print('Request failed with status: ${response.statusCode}');
}
}
} catch (e) {
print('An error occurred: $e');
}
return {};
}