Skip to content

Commit

Permalink
[native_dio_adapter] ✨ Adds createCronetEngine and `createCupertino…
Browse files Browse the repository at this point in the history
…Configuration` (#2040)

...to deprecate `cronetEngine` and `cupertinoConfiguration` for the
`NativeAdapter`, to avoid platform exceptions.

Fixes #2039.

### New Pull Request Checklist

- [x] I have read the
[Documentation](https://pub.dev/documentation/dio/latest/)
- [x] I have searched for a similar pull request in the
[project](https://github.com/cfug/dio/pulls) and found none
- [x] I have updated this branch with the latest `main` branch to avoid
conflicts (via merge from master or rebase)
- [ ] I have added the required tests to prove the fix/feature I'm
adding
- [x] I have updated the documentation (if necessary)
- [x] I have run the tests without failures
- [x] I have updated the `CHANGELOG.md` in the corresponding package
  • Loading branch information
AlexV525 authored Nov 25, 2023
1 parent b191797 commit bb6d65b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
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) {
_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

0 comments on commit bb6d65b

Please sign in to comment.