From d3363cc5c243672162f5695d5f5cb96bbe5b6b96 Mon Sep 17 00:00:00 2001 From: Alex Li Date: Fri, 16 Dec 2022 23:26:48 +0800 Subject: [PATCH] Allow asynchronized method with `savePath` (#36) --- dio/CHANGELOG.md | 1 + dio/lib/src/dio/dio_for_native.dart | 6 +++--- dio/test/download_test.dart | 16 ++++++++++++++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/dio/CHANGELOG.md b/dio/CHANGELOG.md index 1be2b06..c14bf5d 100644 --- a/dio/CHANGELOG.md +++ b/dio/CHANGELOG.md @@ -2,6 +2,7 @@ ## 5.0.0-dev.1 +- Allow asynchronized method with `savePath`. - Allow `data` in all request methods. - A platform independent `HttpClientAdapter` can now be instantiated by doing `dio.httpClientAdapter = HttpClientAdapter();`. diff --git a/dio/lib/src/dio/dio_for_native.dart b/dio/lib/src/dio/dio_for_native.dart index af58b17..8be8d1b 100644 --- a/dio/lib/src/dio/dio_for_native.dart +++ b/dio/lib/src/dio/dio_for_native.dart @@ -64,19 +64,19 @@ class DioForNative with DioMixin implements Dio { rethrow; } final File file; - if (savePath is String Function(Headers)) { + if (savePath is FutureOr Function(Headers)) { // Add real Uri and redirect information to headers. response.headers ..add('redirects', response.redirects.length.toString()) ..add('uri', response.realUri.toString()); - file = File(savePath(response.headers)); + file = File(await savePath(response.headers)); } else if (savePath is String) { file = File(savePath); } else { throw ArgumentError.value( savePath.runtimeType, 'savePath', - 'The type must be `String` or `String Function(Headers)`.', + 'The type must be `String` or `FutureOr Function(Headers)`.', ); } diff --git a/dio/test/download_test.dart b/dio/test/download_test.dart index 231e919..49e0905 100644 --- a/dio/test/download_test.dart +++ b/dio/test/download_test.dart @@ -8,6 +8,7 @@ import 'dart:io'; import 'package:dio/dio.dart'; import 'package:test/test.dart'; +import 'mock/adapters.dart'; import 'utils.dart'; void main() { @@ -93,4 +94,19 @@ void main() { ); //print(r); }); + + test('Test `savePath` types', () async { + Object? error; + final dio = Dio() + ..options.baseUrl = EchoAdapter.mockBase + ..httpClientAdapter = EchoAdapter(); + try { + await dio.download('/test', 'testPath'); + await dio.download('/test', (headers) => 'testPath'); + await dio.download('/test', (headers) async => 'testPath'); + } catch (e) { + error = e; + } + expect(error, null); + }); }