text
stringlengths
1
372
<topic_start>
networking
<topic_end>
<topic_start>
cross-platform http networking
the http package provides the simplest way to issue http requests. this
package is supported on android, iOS, macOS, windows, linux and the web.
<topic_end>
<topic_start>
platform notes
some platforms require additional steps, as detailed below.
<topic_end>
<topic_start>
android
android apps must declare their use of the internet in the android
manifest (androidmanifest.xml):
<topic_end>
<topic_start>
macOS
macOS apps must allow network access in the relevant *.entitlements files.
learn more about setting up entitlements.
<topic_end>
<topic_start>
samples
for a practical sample of various networking tasks (incl. fetching data,
WebSockets, and parsing data in the background) see the
networking cookbook.
<topic_end>
<topic_start>
fetch data from the internet
fetching data from the internet is necessary for most apps.
luckily, dart and flutter provide tools, such as the
http package, for this type of work.
info note
you should avoid directly using dart:io or dart:html
to make HTTP requests.
those libraries are platform-dependent
and tied to a single implementation.
this recipe uses the following steps:
<topic_end>
<topic_start>
1. add the http package
the http package provides the
simplest way to fetch data from the internet.
to add the http package as a dependency,
run flutter pub add:
import the http package.
<code_start>
import 'package:http/http.dart' as http;
<code_end>
if you are deploying to android, edit your AndroidManifest.xml file to
add the internet permission.
likewise, if you are deploying to macOS, edit your
macos/Runner/DebugProfile.entitlements and macos/Runner/Release.entitlements
files to include the network client entitlement.
<topic_end>
<topic_start>
2. make a network request
this recipe covers how to fetch a sample album from the
JSONPlaceholder using the http.get() method.
<code_start>
Future<http.Response> fetchAlbum() {
return http.get(Uri.parse('https://jsonplaceholder.typicode.com/albums/1'));
}
<code_end>
the http.get() method returns a future that contains a response.
<topic_end>
<topic_start>
3. convert the response into a custom dart object
while it’s easy to make a network request, working with a raw
Future<http.Response> isn’t very convenient.
to make your life easier,
convert the http.Response into a dart object.
<topic_end>
<topic_start>
create an album class
first, create an album class that contains the data from the
network request. it includes a factory constructor that
creates an album from JSON.
converting JSON using pattern matching is only one option.
for more information, see the full article on
JSON and serialization.
<code_start>
class album {
final int userId;
final int id;
final string title;
const album({
required this.userId,
required this.id,
required this.title,
});
factory Album.fromJson(Map<String, dynamic> json) {
return switch (json) {
{
'userid': int userId,
'id': int id,
'title': string title,
} =>
album(