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

[native_dio_adapter] ✨ Adds createCronetEngine and createCupertinoConfiguration #2040

Merged
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
4 changes: 3 additions & 1 deletion plugins/native_dio_adapter/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

## Unreleased

*None.*
- Adds `createCronetEngine` and `createCupertinoConfiguration`
to deprecate `cronetEngine` and `cupertinoConfiguration`
for the `NativeAdapter`, to avoid platform exceptions.

## 1.1.1

Expand Down
4 changes: 1 addition & 3 deletions plugins/native_dio_adapter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ Add the `native_dio_adapter` package to your

```dart
final dioClient = Dio();
if (Platform.isIOS || Platform.isMacOS || Platform.isAndroid) {
dioClient.httpClientAdapter = NativeAdapter();
}
dioClient.httpClientAdapter = NativeAdapter();
```

## 📣 About the author
Expand Down
4 changes: 2 additions & 2 deletions plugins/native_dio_adapter/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class _MyHomePageState extends State<MyHomePage> {
final dio = Dio();

dio.httpClientAdapter = NativeAdapter(
cupertinoConfiguration:
createCupertinoConfiguration: () =>
URLSessionConfiguration.ephemeralSessionConfiguration()
..allowsCellularAccess = false
..allowsConstrainedNetworkAccess = false
Expand Down Expand Up @@ -101,7 +101,7 @@ class _MyHomePageState extends State<MyHomePage> {
final dio = Dio();

dio.httpClientAdapter = NativeAdapter(
cupertinoConfiguration:
createCupertinoConfiguration: () =>
URLSessionConfiguration.ephemeralSessionConfiguration()
..allowsCellularAccess = false
..allowsConstrainedNetworkAccess = false
Expand Down
19 changes: 17 additions & 2 deletions plugins/native_dio_adapter/lib/src/native_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,29 @@ import 'cupertino_adapter.dart';
/// make HTTP requests.
class NativeAdapter implements HttpClientAdapter {
NativeAdapter({
CronetEngine Function()? createCronetEngine,
URLSessionConfiguration Function()? createCupertinoConfiguration,
@Deprecated(
'Use createCronetEngine instead. '
'This will cause platform exception on iOS/macOS platforms. '
'This will be removed in v2.0.0',
)
CronetEngine? androidCronetEngine,
@Deprecated(
'Use createCupertinoConfiguration instead. '
'This will cause platform exception on the Android platform. '
'This will be removed in v2.0.0',
)
URLSessionConfiguration? cupertinoConfiguration,
}) {
if (Platform.isAndroid) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Unrelated to this PR, but I guess we could make use of https://pub.dev/packages/platform here later to enable better testing in the future

_adapter = CronetAdapter(androidCronetEngine);
_adapter = CronetAdapter(
createCronetEngine?.call() ?? androidCronetEngine,
);
} else if (Platform.isIOS || Platform.isMacOS) {
_adapter = CupertinoAdapter(
cupertinoConfiguration ??
createCupertinoConfiguration?.call() ??
cupertinoConfiguration ??
URLSessionConfiguration.defaultSessionConfiguration(),
);
} else {
Expand Down