|
| 1 | +[](https://pub.dev/packages/cronet_http) |
| 2 | +[](https://pub.dev/packages/cronet_http/publisher) |
| 3 | + |
1 | 4 | An Android Flutter plugin that provides access to the
|
2 |
| -[Cronet](https://developer.android.com/guide/topics/connectivity/cronet/reference/org/chromium/net/package-summary) |
| 5 | +[Cronet][] |
3 | 6 | HTTP client.
|
4 | 7 |
|
5 | 8 | Cronet is available as part of
|
6 |
| -[Google Play Services](https://developers.google.com/android/guides/overview). |
| 9 | +[Google Play Services][]. |
7 | 10 |
|
8 |
| -This package depends on |
9 |
| -[Google Play Services](https://developers.google.com/android/guides/overview) |
10 |
| -for its Cronet implementation. |
| 11 | +This package depends on [Google Play Services][] for its [Cronet][] |
| 12 | +implementation. |
11 | 13 | [`package:cronet_http_embedded`](https://pub.dev/packages/cronet_http_embedded)
|
12 |
| -is functionally identical to this package but embeds Cronet directly instead |
13 |
| -of relying on |
14 |
| -[Google Play Services](https://developers.google.com/android/guides/overview). |
15 |
| - |
16 |
| -## Status: Experimental |
17 |
| - |
18 |
| -**NOTE**: This package is currently experimental and published under the |
19 |
| -[labs.dart.dev](https://dart.dev/dart-team-packages) pub publisher in order to |
20 |
| -solicit feedback. |
21 |
| - |
22 |
| -For packages in the labs.dart.dev publisher we generally plan to either graduate |
23 |
| -the package into a supported publisher (dart.dev, tools.dart.dev) after a period |
24 |
| -of feedback and iteration, or discontinue the package. These packages have a |
25 |
| -much higher expected rate of API and breaking changes. |
26 |
| - |
27 |
| -Your feedback is valuable and will help us evolve this package. |
28 |
| -For general feedback and suggestions please comment in the |
29 |
| -[feedback issue](https://github.com/dart-lang/http/issues/764). |
30 |
| -For bugs, please file an issue in the |
31 |
| -[bug tracker](https://github.com/dart-lang/http/issues). |
| 14 | +is functionally identical to this package but embeds [Cronet][] directly |
| 15 | +instead of relying on [Google Play Services][]. |
| 16 | + |
| 17 | +## Motivation |
| 18 | + |
| 19 | +Using [Cronet][], rather than the socket-based [dart:io HttpClient][] |
| 20 | +implemententation, has several advantages: |
| 21 | + |
| 22 | +1. It automatically supports Android platform features such as HTTP proxies. |
| 23 | +2. It supports configurable caching. |
| 24 | +3. It supports more HTTP features such as HTTP/3. |
| 25 | + |
| 26 | +## Using |
| 27 | + |
| 28 | +The easiest way to use this library is via the the high-level interface |
| 29 | +defined by [package:http Client][]. |
| 30 | + |
| 31 | +This approach allows the same HTTP code to be used on all platforms, while |
| 32 | +still allowing platform-specific setup. |
| 33 | + |
| 34 | +```dart |
| 35 | +import 'package:cronet_http/cronet_http.dart'; |
| 36 | +import 'package:http/http.dart'; |
| 37 | +import 'package:http/io_client.dart'; |
| 38 | +
|
| 39 | +void main() async { |
| 40 | + late Client httpClient; |
| 41 | + if (Platform.isAndroid) { |
| 42 | + final engine = CronetEngine.build( |
| 43 | + cacheMode: CacheMode.memory, |
| 44 | + cacheMaxSize: 2 * 1024 * 1024, |
| 45 | + userAgent: 'Book Agent'); |
| 46 | + httpClient = CronetClient.fromCronetEngine(engine); |
| 47 | + } else { |
| 48 | + httpClient = IOClient(HttpClient()..userAgent = 'Book Agent'); |
| 49 | + } |
| 50 | +
|
| 51 | + final response = await client.get(Uri.https( |
| 52 | + 'www.googleapis.com', |
| 53 | + '/books/v1/volumes', |
| 54 | + {'q': 'HTTP', 'maxResults': '40', 'printType': 'books'})); |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +[Cronet]: https://developer.android.com/guide/topics/connectivity/cronet/reference/org/chromium/net/package-summary |
| 59 | +[dart:io HttpClient]: https://api.dart.dev/stable/dart-io/HttpClient-class.html |
| 60 | +[Google Play Services]: https://developers.google.com/android/guides/overview |
| 61 | +[package:http Client]: https://pub.dev/documentation/http/latest/http/Client-class.html |
0 commit comments