text
stringlengths
1
80
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鈥檛 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鈥檚 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