text
stringlengths
1
372
}
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>
how do i make network requests?
in Xamarin.Forms you would use HttpClient.
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);