From bb6d65bab0277716932205aea0194e6a68849f87 Mon Sep 17 00:00:00 2001 From: Alex Li Date: Sat, 25 Nov 2023 19:13:45 +0800 Subject: [PATCH] =?UTF-8?q?[native=5Fdio=5Fadapter]=20=E2=9C=A8=20Adds=20`?= =?UTF-8?q?createCronetEngine`=20and=20`createCupertinoConfiguration`=20(#?= =?UTF-8?q?2040)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ...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 --- plugins/native_dio_adapter/CHANGELOG.md | 4 +++- plugins/native_dio_adapter/README.md | 4 +--- .../native_dio_adapter/example/lib/main.dart | 4 ++-- .../lib/src/native_adapter.dart | 19 +++++++++++++++++-- 4 files changed, 23 insertions(+), 8 deletions(-) diff --git a/plugins/native_dio_adapter/CHANGELOG.md b/plugins/native_dio_adapter/CHANGELOG.md index c68a5d717..7e9bdaf7e 100644 --- a/plugins/native_dio_adapter/CHANGELOG.md +++ b/plugins/native_dio_adapter/CHANGELOG.md @@ -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 diff --git a/plugins/native_dio_adapter/README.md b/plugins/native_dio_adapter/README.md index d79ba0cbe..aab6c498b 100644 --- a/plugins/native_dio_adapter/README.md +++ b/plugins/native_dio_adapter/README.md @@ -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 diff --git a/plugins/native_dio_adapter/example/lib/main.dart b/plugins/native_dio_adapter/example/lib/main.dart index 69a69cab2..fe0391dd5 100644 --- a/plugins/native_dio_adapter/example/lib/main.dart +++ b/plugins/native_dio_adapter/example/lib/main.dart @@ -70,7 +70,7 @@ class _MyHomePageState extends State { final dio = Dio(); dio.httpClientAdapter = NativeAdapter( - cupertinoConfiguration: + createCupertinoConfiguration: () => URLSessionConfiguration.ephemeralSessionConfiguration() ..allowsCellularAccess = false ..allowsConstrainedNetworkAccess = false @@ -101,7 +101,7 @@ class _MyHomePageState extends State { final dio = Dio(); dio.httpClientAdapter = NativeAdapter( - cupertinoConfiguration: + createCupertinoConfiguration: () => URLSessionConfiguration.ephemeralSessionConfiguration() ..allowsCellularAccess = false ..allowsConstrainedNetworkAccess = false diff --git a/plugins/native_dio_adapter/lib/src/native_adapter.dart b/plugins/native_dio_adapter/lib/src/native_adapter.dart index bd25f30e1..9bf31ac27 100644 --- a/plugins/native_dio_adapter/lib/src/native_adapter.dart +++ b/plugins/native_dio_adapter/lib/src/native_adapter.dart @@ -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 {