text stringlengths 1 474 |
|---|
), |
); |
} |
}<code_end> |
<topic_end> |
<topic_start>Make authenticated requests |
To fetch data from most web services, you need to provide |
authorization. There are many ways to do this, |
but perhaps the most common uses the Authorization HTTP header.<topic_end> |
<topic_start> |
Add authorization headers |
The http package provides a |
convenient way to add headers to your requests. |
Alternatively, use the HttpHeaders |
class from the dart:io library. |
<code_start>final response = await http.get( |
Uri.parse('https://jsonplaceholder.typicode.com/albums/1'), |
// Send authorization headers to the backend. |
headers: { |
HttpHeaders.authorizationHeader: 'Basic your_api_token_here', |
}, |
);<code_end> |
<topic_end> |
<topic_start> |
Complete example |
This example builds upon the |
Fetching data from the internet recipe. |
<code_start>import 'dart:async'; |
import 'dart:convert'; |
import 'dart:io'; |
import 'package:http/http.dart' as http; |
Future<Album> fetchAlbum() async { |
final response = await http.get( |
Uri.parse('https://jsonplaceholder.typicode.com/albums/1'), |
// Send authorization headers to the backend. |
headers: { |
HttpHeaders.authorizationHeader: 'Basic your_api_token_here', |
}, |
); |
final responseJson = jsonDecode(response.body) as Map<String, dynamic>; |
return Album.fromJson(responseJson); |
} |
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( |
userId: userId, |
id: id, |
title: title, |
), |
_ => throw const FormatException('Failed to load album.'), |
}; |
} |
}<code_end> |
<topic_end> |
<topic_start>Send data to the internet |
Sending data to the internet is necessary for most apps. |
The http package has got that covered, too.This recipe uses the following steps:<topic_end> |
<topic_start> |
1. Add the http package |
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 develop for android, |
add the following permission inside the manifest tag |
in the AndroidManifest.xml file located at android/app/src/main.<topic_end> |
<topic_start> |
2. Sending data to server |
This recipe covers how to create an Album |
by sending an album title to the |
JSONPlaceholder using the |
http.post() method.Import dart:convert for access to jsonEncode to encode the data: |
<code_start>import 'dart:convert';<code_end> |
Use the http.post() method to send the encoded data: |
<code_start>Future<http.Response> createAlbum(String title) { |
return http.post( |
Uri.parse('https://jsonplaceholder.typicode.com/albums'), |
headers: <String, String>{ |
'Content-Type': 'application/json; charset=UTF-8', |
}, |
body: jsonEncode(<String, String>{ |
'title': title, |
}), |
); |
}<code_end> |
The http.post() method returns a Future that contains a Response.<topic_end> |
<topic_start> |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.