Skip to content

Commit 73b1a4c

Browse files
authored
doc(cupertino_http): fix code examples (#1923)
1 parent d06f6b1 commit 73b1a4c

5 files changed

Lines changed: 25 additions & 6 deletions

File tree

pkgs/cupertino_http/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## 3.0.1-wip
22

3+
* Fix some code examples.
34
* Remove `README.md` text saying that the package is a Flutter plugin.
45

56
## 3.0.0

pkgs/cupertino_http/README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ A macOS/iOS library that provides access to the
99
Using the [Foundation URL Loading System][], rather than the socket-based
1010
[dart:io HttpClient][] implementation, has several advantages:
1111

12-
1. It automatically supports iOS/macOS platform features such VPNs and HTTP
13-
proxies.
12+
1. It automatically supports iOS/macOS platform features such as VPNs and HTTP
13+
proxies.
1414
2. It supports many more configuration options such as only allowing access
1515
through WiFi and blocking cookies.
1616
3. It supports more HTTP features such as HTTP/3 and custom redirect handling.
@@ -24,6 +24,8 @@ This approach allows the same HTTP code to be used on all platforms, while
2424
still allowing platform-specific setup.
2525

2626
```dart
27+
import 'dart:io';
28+
2729
import 'package:cupertino_http/cupertino_http.dart';
2830
import 'package:http/http.dart';
2931
import 'package:http/io_client.dart';
@@ -43,6 +45,8 @@ void main() async {
4345
'www.googleapis.com',
4446
'/books/v1/volumes',
4547
{'q': 'HTTP', 'maxResults': '40', 'printType': 'books'}));
48+
49+
httpClient.close();
4650
}
4751
```
4852

@@ -81,8 +85,8 @@ final url = Uri.https(
8185
final session = URLSession.sharedSession();
8286
final task = session.dataTaskWithCompletionHandler(URLRequest.fromUrl(url),
8387
(data, response, error) {
84-
if (error == null && response!.statusCode == 200) {
85-
print(data!.bytes);
88+
if (error == null && response is HTTPURLResponse && response.statusCode == 200) {
89+
print(data!.toList());
8690
}
8791
});
8892
task.resume();

pkgs/cupertino_http/lib/cupertino_http.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
/// [CupertinoClient].
1818
///
1919
/// ```
20+
/// import 'dart:convert';
21+
/// import 'dart:io';
22+
/// import 'dart:math';
23+
///
2024
/// import 'package:cupertino_http/cupertino_http.dart';
2125
///
2226
/// void main() async {
@@ -35,6 +39,7 @@
3539
/// for (var i = 0; i < min(itemCount, 10); ++i) {
3640
/// print(decodedResponse['items'][i]['volumeInfo']['title']);
3741
/// }
42+
/// client.close();
3843
/// }
3944
/// ```
4045
///
@@ -43,6 +48,8 @@
4348
/// platform.
4449
///
4550
/// ```
51+
/// import 'dart:io';
52+
///
4653
/// void main() {
4754
/// final Client httpClient;
4855
/// if (Platform.isIOS || Platform.isMacOS) {
@@ -75,7 +82,7 @@
7582
/// URLRequest.fromUrl(url),
7683
/// (data, response, error) {
7784
/// if (error == null) {
78-
/// if (response != null && response.statusCode == 200) {
85+
/// if (response is HTTPURLResponse && response.statusCode == 200) {
7986
/// print(response); // Do something with the response.
8087
/// return;
8188
/// }

pkgs/cupertino_http/lib/src/cupertino_api.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
/// URLRequest.fromUrl(url),
1515
/// (data, response, error) {
1616
/// if (error == null) {
17-
/// if (response != null && response.statusCode == 200) {
17+
/// if (response is HTTPURLResponse && response.statusCode == 200) {
1818
/// print(response); // Do something with the response.
1919
/// return;
2020
/// }

pkgs/cupertino_http/lib/src/cupertino_client.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ class _StreamedResponseWithUrl extends StreamedResponse
5959
///
6060
/// For example:
6161
/// ```
62+
/// import 'dart:convert';
63+
/// import 'dart:io';
64+
/// import 'dart:math';
65+
///
66+
/// import 'package:cupertino_http/cupertino_http.dart';
67+
///
6268
/// void main() async {
6369
/// var client = CupertinoClient.defaultSessionConfiguration();
6470
/// final response = await client.get(
@@ -75,6 +81,7 @@ class _StreamedResponseWithUrl extends StreamedResponse
7581
/// for (var i = 0; i < min(itemCount, 10); ++i) {
7682
/// print(decodedResponse['items'][i]['volumeInfo']['title']);
7783
/// }
84+
/// client.close();
7885
/// }
7986
/// ```
8087
class CupertinoClient extends BaseClient {

0 commit comments

Comments
 (0)