Skip to content

Commit c09eda7

Browse files
committed
fix cfug#357
1 parent f21d402 commit c09eda7

File tree

7 files changed

+39
-60
lines changed

7 files changed

+39
-60
lines changed

example/cancel_request.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ main() async {
4141
if (CancelToken.isCancel(e)) {
4242
print('$url3: $e');
4343
}
44+
print(e);
4445
})
4546
]);
4647
}

example/test.dart

Lines changed: 10 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,13 @@
1-
import 'dart:convert';
2-
import 'dart:io';
31
import 'package:dio/dio.dart';
2+
void getHttp() async {
3+
try {
4+
Response response = await Dio().get("http://www.google.com");
5+
print(response);
6+
} catch (e) {
7+
print(e);
8+
}
9+
}
410

5-
main() async {
6-
// var formData = new FormData.from({
7-
// "param1": "-1",
8-
// "param1": "-1",
9-
// "param2": "-1",
10-
// "param3": "-1",
11-
// "param4": "-1",
12-
// "param5": null,
13-
// "param8": {"a": "b", "b": "c"},
14-
// "dd":["X",4]
15-
// //"music": new UploadFileInfo(new File("./example/bee.mp4"), "be.mp4"),
16-
// });
17-
//
18-
// //print(formData);
19-
//
20-
// var t = await formData.asBytesAsync();
21-
// print("formStreamSize = ${t.length}");
22-
// print("formData.length = ${formData.length}");
23-
// print("asBytes.length = ${formData.asBytes().length}");
24-
// print(utf8.decode(t)==formData.toString());
25-
26-
Response response;
27-
//upload a video
28-
// response = await Dio().post(
29-
// "http://localhost:3000/upload",
30-
// data: FormData.from({
31-
// "test":"haha",
32-
// "file": UploadFileInfo(File("./example/bee.mp4"), "bee.mp4"),
33-
// "file2": UploadFileInfo(File("./example/upload.txt"), "xx.text"),
34-
// "x":[5,"f"]
35-
// }),
36-
// onSendProgress: (received, total) {
37-
// if (total != -1) {
38-
// print((received / total * 100).toStringAsFixed(0) + "%");
39-
// }
40-
// },
41-
// );
42-
43-
response = await Dio().get("https://google.com", options: Options(connectTimeout:1000));
44-
print(response);
45-
11+
main() async{
12+
await getHttp();
4613
}

package_src/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# 2.1.9
2+
3+
- support flutter version>=1.8 (fix #357)
4+
5+
16
# 2.1.8
27

38
- fix #354 #312

package_src/lib/src/adapter.dart

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'dart:convert';
22
import 'dart:io';
33
import 'dart:async';
4+
import 'dart:typed_data';
45
import 'options.dart';
56
import 'dio_error.dart';
67

@@ -59,7 +60,7 @@ class ResponseBody {
5960
});
6061

6162
/// The response stream
62-
Stream<List<int>> stream;
63+
Stream<Uint8List> stream;
6364

6465
/// the response headers
6566
HttpHeaders headers;
@@ -85,19 +86,20 @@ class ResponseBody {
8586
this.headers,
8687
this.statusMessage,
8788
this.redirects,
88-
}) : stream = Stream.fromIterable(utf8.encode(text).map((e) => [e]).toList());
89+
}) : stream = Stream.fromIterable(utf8.encode(text).map((e) => Uint8List.fromList([e])).toList());
8990

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

99100
/// The default HttpClientAdapter for Dio is [DefaultHttpClientAdapter].
100101
class DefaultHttpClientAdapter extends HttpClientAdapter {
102+
Uint8List E;
101103
Future<ResponseBody> fetch(
102104
RequestOptions options,
103105
Stream<List<int>> requestStream,

package_src/lib/src/dio.dart

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'dart:async';
22
import 'dart:convert';
33
import 'dart:io';
44
import 'dart:math' as math;
5+
import 'dart:typed_data';
56
import 'cancel_token.dart';
67
import 'dio_error.dart';
78
import 'form_data.dart';
@@ -449,8 +450,8 @@ class Dio {
449450
Future future = completer.future;
450451
int received = 0;
451452

452-
// Stream<List<int>>
453-
Stream<List<int>> stream = response.data.stream;
453+
// Stream<Uint8List>
454+
Stream<Uint8List> stream = response.data.stream;
454455
bool compressed = false;
455456
int total = 0;
456457
String contentEncoding =
@@ -493,7 +494,7 @@ class Dio {
493494
}).catchError((derr) async {
494495
try {
495496
await subscription.cancel();
496-
497+
497498
} finally {
498499
completer.completeError(_assureDioError(derr));
499500
}
@@ -825,16 +826,16 @@ class Dio {
825826
}
826827
}
827828

828-
Future<Stream<List<int>>> _transformData(RequestOptions options) async {
829+
Future<Stream<Uint8List>> _transformData(RequestOptions options) async {
829830
var data = options.data;
830-
List<int> bytes;
831-
Stream<List<int>> stream;
831+
Uint8List bytes;
832+
Stream<Uint8List> stream;
832833
if (data != null && ["POST", "PUT", "PATCH", "DELETE"].contains(options.method)) {
833834
// Handle the FormData
834835
int length;
835836
if (data is Stream) {
836-
assert(data is Stream<List<int>>,
837-
"Stream type must be `Stream<List<int>>`, but ${data.runtimeType} is found.");
837+
assert(data is Stream<Uint8List>,
838+
"Stream type must be `Stream<Uint8List>`, but ${data.runtimeType} is found.");
838839
stream = data;
839840
options.headers.keys.any((String key) {
840841
if (key.toLowerCase() == HttpHeaders.contentLengthHeader) {
@@ -848,7 +849,9 @@ class Dio {
848849
options.headers[HttpHeaders.contentTypeHeader] =
849850
'multipart/form-data; boundary=${data.boundary.substring(2)}';
850851
}
851-
stream = data.stream;
852+
stream = data.stream.transform(StreamTransformer<List<int>,Uint8List>.fromHandlers(handleData: (data, sink) {
853+
sink.add(data);
854+
}));
852855
length = data.length;
853856
} else {
854857
// Call request transformer.
@@ -862,7 +865,7 @@ class Dio {
862865
// support data sending progress
863866
length = bytes.length;
864867

865-
var group = new List<List<int>>();
868+
var group = new List<Uint8List>();
866869
const size = 1024;
867870
int groupCount = (bytes.length / size).ceil();
868871
for (int i = 0; i < groupCount; ++i) {
@@ -877,7 +880,7 @@ class Dio {
877880
options.headers[HttpHeaders.contentLengthHeader] = length;
878881
}
879882
int complete = 0;
880-
Stream<List<int>> byteStream =
883+
Stream<Uint8List> byteStream =
881884
stream.transform(StreamTransformer.fromHandlers(
882885
handleData: (data, sink) {
883886
if (options.cancelToken != null && options.cancelToken.isCancelled) {

package_src/lib/src/transformer.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'dart:async';
22
import 'dart:convert';
33
import 'dart:io';
4+
import 'dart:typed_data';
45
import 'dio_error.dart';
56
import 'options.dart';
67
import 'adapter.dart';
@@ -99,8 +100,8 @@ class DefaultTransformer extends Transformer {
99100
response.headers.value(HttpHeaders.contentLengthHeader) ?? "-1");
100101
}
101102
Completer completer = new Completer();
102-
Stream<List<int>> stream = response.stream.transform<List<int>>(
103-
StreamTransformer.fromHandlers(handleData: (data, sink) {
103+
Stream stream = response.stream.transform(
104+
StreamTransformer<Uint8List, List<int>>.fromHandlers(handleData: (data, sink) {
104105
sink.add(data);
105106
if (showDownloadProgress) {
106107
received += data.length;

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.8
3+
version: 2.1.9
44
homepage: https://github.com/flutterchina/dio
55
author: wendux <[email protected]>
66

0 commit comments

Comments
 (0)