text
stringlengths
1
474
'https://jsonplaceholder.typicode.com/posts',
);
final http.Response response = await http.get(dataURL);
setState(() {
data = jsonDecode(response.body);
});
}
Widget getRow(int index) {
return Padding(
padding: const EdgeInsets.all(10),
child: Text('Row ${data[index]['title']}'),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Sample App')),
body: ListView.builder(
itemCount: data.length,
itemBuilder: (context, index) {
return getRow(index);
},
),
);
}
}<code_end>
Refer to the next section for more information
on doing work in the background,
and how Flutter differs from Android.<topic_end>
<topic_start>
How do you move work to a background thread?
Since Flutter is single threaded and runs an event loop,
you don’t have to worry about thread management
or spawning background threads.
This is very similar to Xamarin.Forms.
If you’re doing I/O-bound work, such as disk access or a network call,
then you can safely use async/await and you’re all set.If, on the other hand, you need to do computationally intensive work
that keeps the CPU busy,
you want to move it to an Isolate to avoid blocking the event loop,
like you would keep any sort of work out of the main thread.
This is similar to when you move things to a different
thread via Task.Run() in Xamarin.Forms.For I/O-bound work, declare the function as an async function,
and await on long-running tasks inside the function:
<code_start>Future<void> loadData() async {
final Uri dataURL = Uri.parse(
'https://jsonplaceholder.typicode.com/posts',
);
final http.Response response = await http.get(dataURL);
setState(() {
data = jsonDecode(response.body);
});
}<code_end>
This is how you would typically do network or database calls,
which are both I/O operations.However, there are times when you might be processing
a large amount of data and your UI hangs.
In Flutter, use Isolates to take advantage of multiple CPU cores
to do long-running or computationally intensive tasks.Isolates are separate execution threads that
do not share any memory with the main execution memory heap.
This is a difference between Task.Run().
This means you can’t access variables from the main thread,
or update your UI by calling setState().The following example shows, in a simple isolate,
how to share data back to the main thread to update the UI.
<code_start>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>>>;
}<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,