Skip to content

Commit fece5b3

Browse files
authored
Document that widgets must be initialized before using the cronet_http (#1262)
1 parent 3be8288 commit fece5b3

File tree

4 files changed

+27
-66
lines changed

4 files changed

+27
-66
lines changed

pkgs/cronet_http/example/lib/main.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import 'book.dart';
1717
void main() {
1818
final Client httpClient;
1919
if (Platform.isAndroid) {
20+
WidgetsFlutterBinding.ensureInitialized();
2021
final engine = CronetEngine.build(
2122
cacheMode: CacheMode.memory,
2223
cacheMaxSize: 2 * 1024 * 1024,

pkgs/cronet_http/lib/cronet_http.dart

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,50 +6,41 @@
66
/// [Cronet](https://developer.android.com/guide/topics/connectivity/cronet/reference/org/chromium/net/package-summary)
77
/// HTTP client.
88
///
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).
3014
///
3115
/// [CronetClient] is an implementation of the `package:http` [Client],
3216
/// which means that it can easily used conditionally based on the current
3317
/// platform.
3418
///
3519
/// ```
20+
/// import 'package:provider/provider.dart';
21+
///
3622
/// void main() {
37-
/// var clientFactory = Client.new; // Constructs the default client.
23+
/// final Client httpClient;
3824
/// 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()));
4541
/// }
46-
/// runWithClient(() => runApp(const MyFlutterApp()), clientFactory);
4742
/// }
4843
/// ```
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.
5344
library;
5445

5546
import 'package:http/http.dart';

pkgs/cronet_http/lib/src/cronet_client.dart

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,6 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
/// An Android Flutter plugin that provides access to the
6-
/// [Cronet](https://developer.android.com/guide/topics/connectivity/cronet/reference/org/chromium/net/package-summary)
7-
/// HTTP client.
8-
///
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).
14-
library;
15-
165
import 'dart:async';
176

187
import 'package:http/http.dart';
@@ -296,28 +285,6 @@ jb.UrlRequestCallbackProxy_UrlRequestCallbackInterface _urlRequestCallbacks(
296285
/// A HTTP [Client] based on the
297286
/// [Cronet](https://developer.android.com/guide/topics/connectivity/cronet)
298287
/// network stack.
299-
///
300-
/// For example:
301-
/// ```
302-
/// void main() async {
303-
/// var client = CronetClient.defaultCronetEngine();
304-
/// final response = await client.get(
305-
/// Uri.https('www.googleapis.com', '/books/v1/volumes', {'q': '{http}'}));
306-
/// if (response.statusCode != 200) {
307-
/// throw HttpException('bad response: ${response.statusCode}');
308-
/// }
309-
///
310-
/// final decodedResponse =
311-
/// jsonDecode(utf8.decode(response.bodyBytes)) as Map;
312-
///
313-
/// final itemCount = decodedResponse['totalItems'];
314-
/// print('Number of books about http: $itemCount.');
315-
/// for (var i = 0; i < min(itemCount, 10); ++i) {
316-
/// print(decodedResponse['items'][i]['volumeInfo']['title']);
317-
/// }
318-
/// }
319-
/// ```
320-
///
321288
class CronetClient extends BaseClient {
322289
static final _executor = jb.Executors.newCachedThreadPool();
323290
CronetEngine? _engine;

pkgs/flutter_http_example/lib/http_client_factory.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ import 'dart:io';
66

77
import 'package:cronet_http/cronet_http.dart';
88
import 'package:cupertino_http/cupertino_http.dart';
9+
import 'package:flutter/widgets.dart';
910
import 'package:http/http.dart';
1011
import 'package:http/io_client.dart';
1112

1213
const _maxCacheSize = 2 * 1024 * 1024;
1314

1415
Client httpClient() {
1516
if (Platform.isAndroid) {
17+
WidgetsFlutterBinding.ensureInitialized();
1618
final engine = CronetEngine.build(
1719
cacheMode: CacheMode.memory,
1820
cacheMaxSize: _maxCacheSize,

0 commit comments

Comments
 (0)