|
6 | 6 | /// [Cronet](https://developer.android.com/guide/topics/connectivity/cronet/reference/org/chromium/net/package-summary)
|
7 | 7 | /// HTTP client.
|
8 | 8 | ///
|
9 |
| -/// ``` |
10 |
| -/// import 'package:cronet_http/cronet_http.dart'; |
11 |
| -/// |
12 |
| -/// void main() async { |
13 |
| -/// var client = CronetClient.defaultCronetEngine(); |
14 |
| -/// final response = await client.get( |
15 |
| -/// Uri.https('www.googleapis.com', '/books/v1/volumes', {'q': '{http}'})); |
16 |
| -/// if (response.statusCode != 200) { |
17 |
| -/// throw HttpException('bad response: ${response.statusCode}'); |
18 |
| -/// } |
19 |
| -/// |
20 |
| -/// final decodedResponse = |
21 |
| -/// jsonDecode(utf8.decode(response.bodyBytes)) as Map; |
22 |
| -/// |
23 |
| -/// final itemCount = decodedResponse['totalItems']; |
24 |
| -/// print('Number of books about http: $itemCount.'); |
25 |
| -/// for (var i = 0; i < min(itemCount, 10); ++i) { |
26 |
| -/// print(decodedResponse['items'][i]['volumeInfo']['title']); |
27 |
| -/// } |
28 |
| -/// } |
29 |
| -/// ``` |
| 9 | +/// The platform interface must be initialized before using this plugin e.g. by |
| 10 | +/// calling |
| 11 | +/// [`WidgetsFlutterBinding.ensureInitialized`](https://api.flutter.dev/flutter/widgets/WidgetsFlutterBinding/ensureInitialized.html) |
| 12 | +/// or |
| 13 | +/// [`runApp`](https://api.flutter.dev/flutter/widgets/runApp.html). |
30 | 14 | ///
|
31 | 15 | /// [CronetClient] is an implementation of the `package:http` [Client],
|
32 | 16 | /// which means that it can easily used conditionally based on the current
|
33 | 17 | /// platform.
|
34 | 18 | ///
|
35 | 19 | /// ```
|
| 20 | +/// import 'package:provider/provider.dart'; |
| 21 | +/// |
36 | 22 | /// void main() {
|
37 |
| -/// var clientFactory = Client.new; // Constructs the default client. |
| 23 | +/// final Client httpClient; |
38 | 24 | /// if (Platform.isAndroid) {
|
39 |
| -/// Future<CronetEngine>? engine; |
40 |
| -/// clientFactory = () { |
41 |
| -/// engine ??= CronetEngine.build( |
42 |
| -/// cacheMode: CacheMode.memory, userAgent: 'MyAgent'); |
43 |
| -/// return CronetClient.fromCronetEngineFuture(engine!); |
44 |
| -/// }; |
| 25 | +/// // `package:cronet_http` cannot be used until |
| 26 | +/// // `WidgetsFlutterBinding.ensureInitialized()` or `runApp` is called. |
| 27 | +/// WidgetsFlutterBinding.ensureInitialized(); |
| 28 | +/// final engine = CronetEngine.build( |
| 29 | +/// cacheMode: CacheMode.memory, |
| 30 | +/// cacheMaxSize: 2 * 1024 * 1024, |
| 31 | +/// userAgent: 'Book Agent'); |
| 32 | +/// httpClient = CronetClient.fromCronetEngine(engine, closeEngine: true); |
| 33 | +/// } else { |
| 34 | +/// httpClient = IOClient(HttpClient()..userAgent = 'Book Agent'); |
| 35 | +/// } |
| 36 | +/// |
| 37 | +/// runApp(Provider<Client>( |
| 38 | +/// create: (_) => httpClient, |
| 39 | +/// child: const BookSearchApp(), |
| 40 | +/// dispose: (_, client) => client.close())); |
45 | 41 | /// }
|
46 |
| -/// runWithClient(() => runApp(const MyFlutterApp()), clientFactory); |
47 | 42 | /// }
|
48 | 43 | /// ```
|
49 |
| -/// |
50 |
| -/// After the above setup, calling [Client] methods or any of the |
51 |
| -/// `package:http` convenient functions (e.g. [get]) will result in |
52 |
| -/// [CronetClient] being used on Android. |
53 | 44 | library;
|
54 | 45 |
|
55 | 46 | import 'package:http/http.dart';
|
|
0 commit comments