2
2
[ ![ package publisher] ( https://img.shields.io/pub/publisher/cronet_http.svg )] ( https://pub.dev/packages/cronet_http/publisher )
3
3
4
4
An Android Flutter plugin that provides access to the
5
- [ Cronet] [ ]
6
- HTTP client.
5
+ [ Cronet] [ ] HTTP client.
7
6
8
- Cronet is available as part of
9
- [ Google Play Services ] [ ] .
7
+ Cronet is available as part of [ Google Play Services ] [ ]
8
+ and as [ a standalone embedded library ] [ ] .
10
9
11
- This package depends on [ Google Play Services] [ ] for its [ Cronet] [ ]
12
- implementation.
13
- [ ` package:cronet_http_embedded ` ] ( https://pub.dev/packages/cronet_http_embedded )
14
- is functionally identical to this package but embeds [ Cronet] [ ] directly
15
- instead of relying on [ Google Play Services] [ ] .
10
+ This package depends on [ Google Play Services] [ ]
11
+ for its [ Cronet] [ ] implementation.
12
+ To use the embedded version of [ Cronet] [ ] without [ Google Play Services] [ ] ,
13
+ see [ Use embedded Cronet] ( #use-embedded-cronet ) .
16
14
17
15
## Motivation
18
16
19
- Using [ Cronet] [ ] , rather than the socket-based [ dart : io HttpClient ] [ ]
20
- implemententation , has several advantages:
17
+ Using [ Cronet] [ ] , rather than the socket-based
18
+ [ dart : io HttpClient ] [ ] implementation , has several advantages:
21
19
22
20
1 . It automatically supports Android platform features such as HTTP proxies.
23
21
2 . It supports configurable caching.
@@ -40,23 +38,46 @@ void main() async {
40
38
final Client httpClient;
41
39
if (Platform.isAndroid) {
42
40
final engine = CronetEngine.build(
43
- cacheMode: CacheMode.memory,
44
- cacheMaxSize: 2 * 1024 * 1024,
45
- userAgent: 'Book Agent');
41
+ cacheMode: CacheMode.memory,
42
+ cacheMaxSize: 2 * 1024 * 1024,
43
+ userAgent: 'Book Agent',
44
+ );
46
45
httpClient = CronetClient.fromCronetEngine(engine, isOwned: true);
47
46
} else {
48
47
httpClient = IOClient(HttpClient()..userAgent = 'Book Agent');
49
48
}
50
49
51
- final response = await client.get(Uri.https(
50
+ final response = await client.get(
51
+ Uri.https(
52
52
'www.googleapis.com',
53
53
'/books/v1/volumes',
54
- {'q': 'HTTP', 'maxResults': '40', 'printType': 'books'}));
54
+ {'q': 'HTTP', 'maxResults': '40', 'printType': 'books'},
55
+ ),
56
+ );
55
57
httpClient.close();
56
58
}
57
59
```
58
60
61
+ ### Use embedded Cronet
62
+
63
+ If you want your application to work without [ Google Play Services] [ ] ,
64
+ you can instead depend on the ` org.chromium.net:cronet-embedded ` package
65
+ by using ` dart-define ` to set ` cronetHttpNoPlay ` is set to ` true ` .
66
+
67
+ For example:
68
+
69
+ ```
70
+ flutter run --dart-define=cronetHttpNoPlay=true
71
+ ```
72
+
73
+ To use the embedded version in ` flutter test ` :
74
+
75
+ ```
76
+ flutter test --dart-define=cronetHttpNoPlay=true
77
+ ```
78
+
59
79
[ Cronet ] : https://developer.android.com/guide/topics/connectivity/cronet/reference/org/chromium/net/package-summary
60
- [ dart:io HttpClient ] : https://api.dart.dev/stable/dart-io/HttpClient-class.html
61
80
[ Google Play Services ] : https://developers.google.com/android/guides/overview
81
+ [ a standalone embedded library ] : https://mvnrepository.com/artifact/org.chromium.net/cronet-embedded
82
+ [ dart:io HttpClient ] : https://api.dart.dev/stable/dart-io/HttpClient-class.html
62
83
[ package:http Client ] : https://pub.dev/documentation/http/latest/http/Client-class.html
0 commit comments