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 {