text
stringlengths 1
474
|
|---|
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 use the http package, add it to your dependencies in pubspec.yaml:To make a network request,
|
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>
|
How do I show the progress for a long-running task?
|
In Xamarin.Forms you would typically create a loading indicator,
|
either directly in XAML or through a 3rd party plugin such as AcrDialogs.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:async';
|
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 const MaterialApp(
|
title: 'Sample App',
|
home: SampleAppPage(),
|
);
|
}
|
}
|
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();
|
}
|
bool get showLoadingDialog => data.isEmpty;
|
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 getBody() {
|
if (showLoadingDialog) {
|
return getProgressDialog();
|
}
|
return getListView();
|
}
|
Widget getProgressDialog() {
|
return const Center(child: CircularProgressIndicator());
|
}
|
ListView getListView() {
|
return ListView.builder(
|
itemCount: data.length,
|
itemBuilder: (context, index) {
|
return getRow(index);
|
},
|
);
|
}
|
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: getBody(),
|
);
|
}
|
}<code_end>
|
<topic_end>
|
<topic_start>
|
Project structure & resources
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.