Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ResponseDecoder return nullable String type #1581

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dio/lib/src/options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ enum ListFormat {

typedef ValidateStatus = bool Function(int? status);

typedef ResponseDecoder = String Function(
typedef ResponseDecoder = String? Function(
List<int> responseBytes, RequestOptions options, ResponseBody responseBody);
typedef RequestEncoder = List<int> Function(
String request, RequestOptions options);
Expand Down
5 changes: 3 additions & 2 deletions dio/lib/src/transformer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,11 @@ class DefaultTransformer extends Transformer {
options,
response..stream = Stream.empty(),
);
} else {
} else if (responseBytes.isNotEmpty) {
responseBody = utf8.decode(responseBytes, allowMalformed: true);
}
if (responseBody.isNotEmpty &&
if (responseBody != null &&
responseBody.isNotEmpty &&
options.responseType == ResponseType.json &&
Transformer.isJsonMimeType(
response.headers[Headers.contentTypeHeader]?.first)) {
Expand Down
11 changes: 11 additions & 0 deletions dio/test/options_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -270,4 +270,15 @@ void main() {
assert(r3.uri.toString() == 'https://www.example.com/test');
assert(r3.headers[Headers.contentTypeHeader] == null);
});

test("#test responseDecoder return null", () async {
final dio = Dio();
dio.options.responseDecoder = (_,__,___) => null;
dio.options.baseUrl = EchoAdapter.mockBase;
dio.httpClientAdapter = EchoAdapter();

final Response response = await dio.get("");

expect(response.data, null);
});
}