Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document that widgets must be initialized before using the cronet_http #1262

Merged
merged 1 commit into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkgs/cronet_http/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
57 changes: 24 additions & 33 deletions pkgs/cronet_http/lib/cronet_http.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<CronetEngine>? 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<Client>(
/// 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';
Expand Down
33 changes: 0 additions & 33 deletions pkgs/cronet_http/lib/src/cronet_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions pkgs/flutter_http_example/lib/http_client_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ 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';

const _maxCacheSize = 2 * 1024 * 1024;

Client httpClient() {
if (Platform.isAndroid) {
WidgetsFlutterBinding.ensureInitialized();
final engine = CronetEngine.build(
cacheMode: CacheMode.memory,
cacheMaxSize: _maxCacheSize,
Expand Down
Loading