From fece5b379ccc15c05c5771298a2dc229605a616b Mon Sep 17 00:00:00 2001 From: Brian Quinlan Date: Thu, 11 Jul 2024 16:05:59 -0700 Subject: [PATCH] Document that widgets must be initialized before using the cronet_http (#1262) --- pkgs/cronet_http/example/lib/main.dart | 1 + pkgs/cronet_http/lib/cronet_http.dart | 57 ++++++++----------- pkgs/cronet_http/lib/src/cronet_client.dart | 33 ----------- .../lib/http_client_factory.dart | 2 + 4 files changed, 27 insertions(+), 66 deletions(-) diff --git a/pkgs/cronet_http/example/lib/main.dart b/pkgs/cronet_http/example/lib/main.dart index 62990e337d..37136a2afb 100644 --- a/pkgs/cronet_http/example/lib/main.dart +++ b/pkgs/cronet_http/example/lib/main.dart @@ -17,6 +17,7 @@ import 'book.dart'; void main() { final Client httpClient; if (Platform.isAndroid) { + WidgetsFlutterBinding.ensureInitialized(); final engine = CronetEngine.build( cacheMode: CacheMode.memory, cacheMaxSize: 2 * 1024 * 1024, diff --git a/pkgs/cronet_http/lib/cronet_http.dart b/pkgs/cronet_http/lib/cronet_http.dart index fe6499fc6d..53c9aca019 100644 --- a/pkgs/cronet_http/lib/cronet_http.dart +++ b/pkgs/cronet_http/lib/cronet_http.dart @@ -6,50 +6,41 @@ /// [Cronet](https://developer.android.com/guide/topics/connectivity/cronet/reference/org/chromium/net/package-summary) /// HTTP client. /// -/// ``` -/// import 'package:cronet_http/cronet_http.dart'; -/// -/// void main() async { -/// var client = CronetClient.defaultCronetEngine(); -/// final response = await client.get( -/// Uri.https('www.googleapis.com', '/books/v1/volumes', {'q': '{http}'})); -/// if (response.statusCode != 200) { -/// throw HttpException('bad response: ${response.statusCode}'); -/// } -/// -/// final decodedResponse = -/// jsonDecode(utf8.decode(response.bodyBytes)) as Map; -/// -/// final itemCount = decodedResponse['totalItems']; -/// print('Number of books about http: $itemCount.'); -/// for (var i = 0; i < min(itemCount, 10); ++i) { -/// print(decodedResponse['items'][i]['volumeInfo']['title']); -/// } -/// } -/// ``` +/// The platform interface must be initialized before using this plugin e.g. by +/// calling +/// [`WidgetsFlutterBinding.ensureInitialized`](https://api.flutter.dev/flutter/widgets/WidgetsFlutterBinding/ensureInitialized.html) +/// or +/// [`runApp`](https://api.flutter.dev/flutter/widgets/runApp.html). /// /// [CronetClient] is an implementation of the `package:http` [Client], /// which means that it can easily used conditionally based on the current /// platform. /// /// ``` +/// import 'package:provider/provider.dart'; +/// /// void main() { -/// var clientFactory = Client.new; // Constructs the default client. +/// final Client httpClient; /// if (Platform.isAndroid) { -/// Future? engine; -/// clientFactory = () { -/// engine ??= CronetEngine.build( -/// cacheMode: CacheMode.memory, userAgent: 'MyAgent'); -/// return CronetClient.fromCronetEngineFuture(engine!); -/// }; +/// // `package:cronet_http` cannot be used until +/// // `WidgetsFlutterBinding.ensureInitialized()` or `runApp` is called. +/// WidgetsFlutterBinding.ensureInitialized(); +/// final engine = CronetEngine.build( +/// cacheMode: CacheMode.memory, +/// cacheMaxSize: 2 * 1024 * 1024, +/// userAgent: 'Book Agent'); +/// httpClient = CronetClient.fromCronetEngine(engine, closeEngine: true); +/// } else { +/// httpClient = IOClient(HttpClient()..userAgent = 'Book Agent'); +/// } +/// +/// runApp(Provider( +/// create: (_) => httpClient, +/// child: const BookSearchApp(), +/// dispose: (_, client) => client.close())); /// } -/// runWithClient(() => runApp(const MyFlutterApp()), clientFactory); /// } /// ``` -/// -/// After the above setup, calling [Client] methods or any of the -/// `package:http` convenient functions (e.g. [get]) will result in -/// [CronetClient] being used on Android. library; import 'package:http/http.dart'; diff --git a/pkgs/cronet_http/lib/src/cronet_client.dart b/pkgs/cronet_http/lib/src/cronet_client.dart index 772f03b0e8..f55617eeb1 100644 --- a/pkgs/cronet_http/lib/src/cronet_client.dart +++ b/pkgs/cronet_http/lib/src/cronet_client.dart @@ -2,17 +2,6 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. -/// An Android Flutter plugin that provides access to the -/// [Cronet](https://developer.android.com/guide/topics/connectivity/cronet/reference/org/chromium/net/package-summary) -/// HTTP client. -/// -/// The platform interface must be initialized before using this plugin e.g. by -/// calling -/// [`WidgetsFlutterBinding.ensureInitialized`](https://api.flutter.dev/flutter/widgets/WidgetsFlutterBinding/ensureInitialized.html) -/// or -/// [`runApp`](https://api.flutter.dev/flutter/widgets/runApp.html). -library; - import 'dart:async'; import 'package:http/http.dart'; @@ -296,28 +285,6 @@ jb.UrlRequestCallbackProxy_UrlRequestCallbackInterface _urlRequestCallbacks( /// A HTTP [Client] based on the /// [Cronet](https://developer.android.com/guide/topics/connectivity/cronet) /// network stack. -/// -/// For example: -/// ``` -/// void main() async { -/// var client = CronetClient.defaultCronetEngine(); -/// final response = await client.get( -/// Uri.https('www.googleapis.com', '/books/v1/volumes', {'q': '{http}'})); -/// if (response.statusCode != 200) { -/// throw HttpException('bad response: ${response.statusCode}'); -/// } -/// -/// final decodedResponse = -/// jsonDecode(utf8.decode(response.bodyBytes)) as Map; -/// -/// final itemCount = decodedResponse['totalItems']; -/// print('Number of books about http: $itemCount.'); -/// for (var i = 0; i < min(itemCount, 10); ++i) { -/// print(decodedResponse['items'][i]['volumeInfo']['title']); -/// } -/// } -/// ``` -/// class CronetClient extends BaseClient { static final _executor = jb.Executors.newCachedThreadPool(); CronetEngine? _engine; diff --git a/pkgs/flutter_http_example/lib/http_client_factory.dart b/pkgs/flutter_http_example/lib/http_client_factory.dart index 6e6ddc040b..51109e520c 100644 --- a/pkgs/flutter_http_example/lib/http_client_factory.dart +++ b/pkgs/flutter_http_example/lib/http_client_factory.dart @@ -6,6 +6,7 @@ import 'dart:io'; import 'package:cronet_http/cronet_http.dart'; import 'package:cupertino_http/cupertino_http.dart'; +import 'package:flutter/widgets.dart'; import 'package:http/http.dart'; import 'package:http/io_client.dart'; @@ -13,6 +14,7 @@ const _maxCacheSize = 2 * 1024 * 1024; Client httpClient() { if (Platform.isAndroid) { + WidgetsFlutterBinding.ensureInitialized(); final engine = CronetEngine.build( cacheMode: CacheMode.memory, cacheMaxSize: _maxCacheSize,