text stringlengths 1 372 |
|---|
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 iOS. |
<topic_end> |
<topic_start> |
moving to the background thread |
since flutter is single threaded and runs an event loop |
(like node.js), you don’t have to worry about |
thread management or spawning background threads. |
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 done. |
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. |
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 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 means you can’t access variables from the main thread, |
or update your UI by calling setState(). |
isolates are true to their name, and cannot share memory |
(in the form of static fields, for example). |
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, |
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; |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.