Skip to content

Commit c7880ae

Browse files
committed
fix cfug#360
1 parent c09eda7 commit c7880ae

File tree

4 files changed

+30
-8
lines changed

4 files changed

+30
-8
lines changed

package_src/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 2.1.10
2+
3+
- fix #360
4+
15
# 2.1.9
26

37
- support flutter version>=1.8 (fix #357)

package_src/lib/src/adapter.dart

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,20 +86,23 @@ class ResponseBody {
8686
this.headers,
8787
this.statusMessage,
8888
this.redirects,
89-
}) : stream = Stream.fromIterable(utf8.encode(text).map((e) => Uint8List.fromList([e])).toList());
89+
}) : stream = Stream.fromIterable(
90+
utf8.encode(text).map((e) => Uint8List.fromList([e])).toList());
9091

9192
ResponseBody.fromBytes(
9293
List<int> bytes,
9394
this.statusCode, {
9495
this.headers,
9596
this.statusMessage,
9697
this.redirects,
97-
}) : stream = Stream.fromIterable(bytes.map((e) => Uint8List.fromList([e])).toList());
98+
}) : stream = Stream.fromIterable(
99+
bytes.map((e) => Uint8List.fromList([e])).toList());
98100
}
99101

100102
/// The default HttpClientAdapter for Dio is [DefaultHttpClientAdapter].
101103
class DefaultHttpClientAdapter extends HttpClientAdapter {
102104
Uint8List E;
105+
103106
Future<ResponseBody> fetch(
104107
RequestOptions options,
105108
Stream<List<int>> requestStream,
@@ -115,8 +118,9 @@ class DefaultHttpClientAdapter extends HttpClientAdapter {
115118
//_httpClient.connectionTimeout= Duration(milliseconds: options.connectTimeout);
116119
// _httpClient.connectionTimeout =
117120
// Duration(milliseconds: options.connectTimeout);
118-
requestFuture = _httpClient.openUrl(options.method, options.uri)
119-
.timeout(Duration(milliseconds: options.connectTimeout));
121+
requestFuture = _httpClient
122+
.openUrl(options.method, options.uri)
123+
.timeout(Duration(milliseconds: options.connectTimeout));
120124
} else {
121125
_httpClient.connectionTimeout = null;
122126
requestFuture = _httpClient.openUrl(options.method, options.uri);
@@ -146,8 +150,7 @@ class DefaultHttpClientAdapter extends HttpClientAdapter {
146150
if (options.receiveTimeout > 0) {
147151
future = future.timeout(Duration(milliseconds: options.receiveTimeout));
148152
}
149-
HttpClientResponse responseStream;
150-
153+
dynamic responseStream;
151154
try {
152155
responseStream = await future;
153156
} on TimeoutException {
@@ -157,8 +160,22 @@ class DefaultHttpClientAdapter extends HttpClientAdapter {
157160
type: DioErrorType.RECEIVE_TIMEOUT,
158161
);
159162
}
163+
164+
Stream<Uint8List> stream;
165+
166+
// https://github.com/dart-lang/co19/issues/383
167+
if (responseStream is Stream<Uint8List>) {
168+
stream = responseStream;
169+
} else {
170+
stream = responseStream.transform(
171+
StreamTransformer<List<int>, Uint8List>.fromHandlers(
172+
handleData: (data, sink) {
173+
sink.add(data);
174+
}));
175+
}
176+
160177
return ResponseBody(
161-
responseStream,
178+
stream,
162179
responseStream.statusCode,
163180
headers: responseStream.headers,
164181
redirects: responseStream.redirects,

package_src/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: dio
22
description: A powerful Http client for Dart, which supports Interceptors, FormData, Request Cancellation, File Downloading, Timeout etc.
3-
version: 2.1.9
3+
version: 2.1.10
44
homepage: https://github.com/flutterchina/dio
55
author: wendux <[email protected]>
66

package_src/test/dio_test.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ void main() {
179179
final url = 'http://download.dcloud.net.cn/HBuilder.9.0.2.macosx_64.dmg';
180180
final savePath = './example/HBuilder.9.0.2.macosx_64.dmg';
181181
await dio.download(url, savePath, cancelToken: token).catchError((e) {
182+
print(e);
182183
expect(CancelToken.isCancel(e), true);
183184
if (CancelToken.isCancel(e)) {
184185
expect(e.type, DioErrorType.CANCEL);

0 commit comments

Comments
 (0)