text
stringlengths 1
474
|
---|
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() {
|
bool showLoadingDialog = data.isEmpty;
|
if (showLoadingDialog) {
|
return getProgressDialog();
|
} else {
|
return getListView();
|
}
|
}
|
Widget getProgressDialog() {
|
return const Center(child: CircularProgressIndicator());
|
}
|
ListView getListView() {
|
return ListView.builder(
|
itemCount: data.length,
|
itemBuilder: (context, position) {
|
return getRow(position);
|
},
|
);
|
}
|
Widget getRow(int i) {
|
return Padding(
|
padding: const EdgeInsets.all(10),
|
child: Text("Row ${data[i]["title"]}"),
|
);
|
}
|
@override
|
Widget build(BuildContext context) {
|
return Scaffold(
|
appBar: AppBar(
|
title: const Text('Sample App'),
|
),
|
body: getBody(),
|
);
|
}
|
}<code_end>
|
<topic_end>
|
<topic_start>
|
Making network requests
|
Making a network call in Flutter is easy when you
|
use the popular http package. This abstracts
|
away a lot of the networking that you might normally
|
implement yourself, making it simple to make network calls.To add the http package as a dependency, run flutter pub add:To make a network call,
|
call await on the async function http.get():
|
<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>
|
<topic_end>
|
<topic_start>
|
Showing the progress on long-running tasks
|
In UIKit, you typically use a UIProgressView
|
while executing a long-running task in the background.In Flutter, use a ProgressIndicator widget.
|
Show the progress programmatically by controlling
|
when it’s rendered through a boolean flag.
|
Tell Flutter to update its state before your long-running task starts,
|
and hide it after it ends.In the example below, the build function is separated into three different
|
functions. If showLoadingDialog is true
|
(when widgets.length == 0), then render the ProgressIndicator.
|
Otherwise, render the ListView with the data returned from a network call.
|
<code_start>import 'dart:convert';
|
import 'package:flutter/material.dart';
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.