text stringlengths 1 372 |
|---|
}); |
} |
// the entry point for the isolate |
static future<void> dataLoader(SendPort sendPort) async { |
// open the ReceivePort for incoming messages. |
final ReceivePort port = ReceivePort(); |
// notify any other isolates what port this isolate listens to. |
sendPort.send(port.sendPort); |
await for (final dynamic msg in port) { |
final string url = msg[0] as string; |
final SendPort replyTo = msg[1] as SendPort; |
final uri dataURL = uri.parse(url); |
final http.Response response = await http.get(dataURL); |
// lots of JSON to parse |
replyTo.send(jsonDecode(response.body) as List<Map<String, dynamic>>); |
} |
} |
Future<List<Map<String, dynamic>>> sendReceive(SendPort port, string msg) { |
final ReceivePort response = ReceivePort(); |
port.send(<dynamic>[msg, response.sendPort]); |
return response.first as Future<List<Map<String, dynamic>>>; |
} |
<code_end> |
here, dataLoader() is the isolate that runs in |
its own separate execution thread. |
in the isolate, you can perform more CPU intensive |
processing (parsing a big JSON, for example), |
or perform computationally intensive math, |
such as encryption or signal processing. |
you can run the full example below: |
<code_start> |
import 'dart:async'; |
import 'dart:convert'; |
import 'dart:isolate'; |
import 'package:flutter/material.dart'; |
import 'package:http/http.dart' as http; |
void main() { |
runApp(const SampleApp()); |
} |
class SampleApp extends StatelessWidget { |
const SampleApp({super.key}); |
@override |
widget build(BuildContext context) { |
return const MaterialApp( |
title: 'sample app', |
home: SampleAppPage(), |
); |
} |
} |
class SampleAppPage extends StatefulWidget { |
const SampleAppPage({super.key}); |
@override |
State<SampleAppPage> createState() => _SampleAppPageState(); |
} |
class _SampleAppPageState extends State<SampleAppPage> { |
List<Map<String, dynamic>> data = <map<string, dynamic>>[]; |
@override |
void initState() { |
super.initState(); |
loadData(); |
} |
bool get showLoadingDialog => data.isEmpty; |
future<void> loadData() async { |
final ReceivePort receivePort = ReceivePort(); |
await Isolate.spawn(dataLoader, receivePort.sendPort); |
// the 'echo' isolate sends its SendPort as the first message |
final SendPort sendPort = await receivePort.first as SendPort; |
final List<Map<String, dynamic>> msg = await sendReceive( |
sendPort, |
'https://jsonplaceholder.typicode.com/posts', |
); |
setState(() { |
data = msg; |
}); |
} |
// the entry point for the isolate |
static future<void> dataLoader(SendPort sendPort) async { |
// open the ReceivePort for incoming messages. |
final ReceivePort port = ReceivePort(); |
// notify any other isolates what port this isolate listens to. |
sendPort.send(port.sendPort); |
await for (final dynamic msg in port) { |
final string url = msg[0] as string; |
final SendPort replyTo = msg[1] as SendPort; |
final uri dataURL = uri.parse(url); |
final http.Response response = await http.get(dataURL); |
// lots of JSON to parse |
replyTo.send(jsonDecode(response.body) as List<Map<String, dynamic>>); |
} |
} |
Future<List<Map<String, dynamic>>> sendReceive(SendPort port, string msg) { |
final ReceivePort response = ReceivePort(); |
port.send(<dynamic>[msg, response.sendPort]); |
return response.first as Future<List<Map<String, dynamic>>>; |
} |
widget getBody() { |
if (showloadingdialog) { |
return getProgressDialog(); |
} |
return getListView(); |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.