diff --git a/dio/lib/src/transformers/sync_transformer.dart b/dio/lib/src/transformers/sync_transformer.dart index 9ddc08c83..e2c6f336c 100644 --- a/dio/lib/src/transformers/sync_transformer.dart +++ b/dio/lib/src/transformers/sync_transformer.dart @@ -125,11 +125,17 @@ class SyncTransformer extends Transformer { ); final String? response; if (options.responseDecoder != null) { - response = await options.responseDecoder!( + final decodeResponse = options.responseDecoder!( responseBytes, options, responseBody..stream = Stream.empty(), ); + + if (decodeResponse is Future) { + response = await decodeResponse; + } else { + response = decodeResponse; + } } else if (!isJsonContent || responseBytes.isNotEmpty) { response = utf8.decode(responseBytes, allowMalformed: true); } else { diff --git a/dio/test/options_test.dart b/dio/test/options_test.dart index 0a30464c6..2c99d2200 100644 --- a/dio/test/options_test.dart +++ b/dio/test/options_test.dart @@ -399,6 +399,28 @@ void main() { expect(response.data, null); }); + test('responseDecoder can return Future', () async { + final dio = Dio(); + dio.options.responseDecoder = (_, __, ___) => Future.value('example'); + dio.options.baseUrl = EchoAdapter.mockBase; + dio.httpClientAdapter = EchoAdapter(); + + final Response response = await dio.get(''); + + expect(response.data, 'example'); + }); + + test('responseDecoder can return String?', () async { + final dio = Dio(); + dio.options.responseDecoder = (_, __, ___) => 'example'; + dio.options.baseUrl = EchoAdapter.mockBase; + dio.httpClientAdapter = EchoAdapter(); + + final Response response = await dio.get(''); + + expect(response.data, 'example'); + }); + test('invalid response type throws exceptions', () async { final dio = Dio( BaseOptions(