text
stringlengths 1
372
|
|---|
);
|
}
|
}
|
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();
|
}
|
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);
|
});
|
}
|
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;
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.