text
stringlengths 1
474
|
---|
} |
Widget getRow(int i) { |
return Padding( |
padding: const EdgeInsets.all(10), |
child: Text("Row ${widgets[i]["title"]}"), |
); |
} |
Future<void> loadData() async { |
ReceivePort receivePort = ReceivePort(); |
await Isolate.spawn(dataLoader, receivePort.sendPort); |
// The 'echo' isolate sends its SendPort as the first message. |
SendPort sendPort = await receivePort.first; |
List msg = await sendReceive( |
sendPort, |
'https://jsonplaceholder.typicode.com/posts', |
); |
setState(() { |
widgets = msg; |
}); |
} |
// The entry point for the isolate. |
static Future<void> dataLoader(SendPort sendPort) async { |
// Open the ReceivePort for incoming messages. |
ReceivePort port = ReceivePort(); |
// Notify any other isolates what port this isolate listens to. |
sendPort.send(port.sendPort); |
await for (var msg in port) { |
String data = msg[0]; |
SendPort replyTo = msg[1]; |
String dataURL = data; |
http.Response response = await http.get(Uri.parse(dataURL)); |
// Lots of JSON to parse |
replyTo.send(jsonDecode(response.body)); |
} |
} |
Future sendReceive(SendPort port, msg) { |
ReceivePort response = ReceivePort(); |
port.send([msg, response.sendPort]); |
return response.first; |
} |
}<code_end> |
<topic_end> |
<topic_start> |
What is the equivalent of OkHttp on Flutter? |
Making a network call in Flutter is easy when you use the |
popular http package.While the http package doesn’t have every feature found in OkHttp, |
it abstracts away much of the networking that you would normally implement |
yourself, making it a simple way 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>import 'dart:developer' as developer; |
import 'package:http/http.dart' as http; |
Future<void> loadData() async { |
var dataURL = Uri.parse('https://jsonplaceholder.typicode.com/posts'); |
http.Response response = await http.get(dataURL); |
developer.log(response.body); |
}<code_end> |
<topic_end> |
<topic_start> |
How do I show the progress for a long-running task? |
In Android you would typically show a ProgressBar view in your UI while |
executing a long-running task on a background thread.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 following example, the build function is separated into three different |
functions. If showLoadingDialog is true (when widgets.isEmpty), |
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'; |
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 MaterialApp( |
title: 'Sample App', |
theme: ThemeData( |
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), |
), |
home: const SampleAppPage(), |
); |
} |
} |
class SampleAppPage extends StatefulWidget { |
const SampleAppPage({super.key}); |
@override |
State<SampleAppPage> createState() => _SampleAppPageState(); |
} |
class _SampleAppPageState extends State<SampleAppPage> { |
List widgets = []; |
@override |
void initState() { |
super.initState(); |
loadData(); |
} |
Widget getBody() { |
bool showLoadingDialog = widgets.isEmpty; |
if (showLoadingDialog) { |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.