Skip to content
Open
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
2 changes: 2 additions & 0 deletions plugins/native_dio_adapter/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

- Support request cancellation for native HTTP clients via use of `AbortableRequest` (introduced in http package from version 1.5.0)
- Add timeout handling for `sendTimeout`, `connectTimeout`, and `receiveTimeout` in `ConversionLayerAdapter`
- Gracefully fall back to `HttpClientAdapter` when Cronet is unavailable on Android (e.g., devices without Google Play Services)
- Add `onCronetUnavailable` callback to `NativeAdapter` for notification when fallback occurs

## 1.5.0

Expand Down
18 changes: 15 additions & 3 deletions plugins/native_dio_adapter/lib/src/native_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import 'cupertino_adapter.dart';
///
/// On Android this uses [cronet_http](https://pub.dev/packages/cronet_http) to
/// make HTTP requests.
///
/// If Cronet is unavailable (e.g., Google Play Services not installed),
/// falls back to the default [HttpClientAdapter] using dart:io.
class NativeAdapter implements HttpClientAdapter {
NativeAdapter({
CronetEngine Function()? createCronetEngine,
Expand All @@ -32,11 +35,20 @@ class NativeAdapter implements HttpClientAdapter {
'This will be removed in v2.0.0',
)
URLSessionConfiguration? cupertinoConfiguration,
void Function(Object error)? onCronetUnavailable,
}) {
if (Platform.isAndroid) {
_adapter = CronetAdapter(
createCronetEngine?.call() ?? androidCronetEngine,
);
try {
_adapter = CronetAdapter(
createCronetEngine?.call() ?? androidCronetEngine,
);
} catch (e) {
// Cronet requires Google Play Services. Fall back to dart:io
// HttpClient when unavailable (e.g., on emulators without Google APIs
// or devices without Google Play Services).
onCronetUnavailable?.call(e);
Comment on lines +45 to +49
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
} catch (e) {
// Cronet requires Google Play Services. Fall back to dart:io
// HttpClient when unavailable (e.g., on emulators without Google APIs
// or devices without Google Play Services).
onCronetUnavailable?.call(e);
} catch (e, s) {
// Cronet requires Google Play Services. Fall back to dart:io
// HttpClient when unavailable (e.g., on emulators without Google APIs
// or devices without Google Play Services).
onCronetUnavailable?.call(e, s);

_adapter = HttpClientAdapter();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As #2444 (comment) said, I think this should be set as opt-in.

}
} else if (Platform.isIOS || Platform.isMacOS) {
_adapter = CupertinoAdapter(
createCupertinoConfiguration?.call() ??
Expand Down