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