-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcancel_request.dart
46 lines (41 loc) · 1.1 KB
/
cancel_request.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import 'dart:async';
import 'package:dio/dio.dart';
main() async {
var dio = new Dio();
// Token can be shared with different requests.
CancelToken token = new CancelToken();
// In one minute, we cancel!
new Timer(new Duration(milliseconds: 200), () {
token.cancel("cancelled");
});
// The follow three requests with the same token.
var url1 = "https://accounts.google.com";
var url2 = "https://www.facebook.com";
var url3 = "https://www.baidu.com";
dio
.get(url1, cancelToken: token)
.then((response) => print('${response.request.path}: succeed!'))
.catchError(
(e) {
if (CancelToken.isCancel(e)) {
print('$url1: $e');
}
},
);
dio
.get(url2, cancelToken: token)
.then((response) => print('${response.request.path}: succeed!'))
.catchError((e) {
if (CancelToken.isCancel(e)) {
print('$url2: $e');
}
});
dio
.get(url3, cancelToken: token)
.then((response) => print('${response.request.path}: succeed!'))
.catchError((e) {
if (CancelToken.isCancel(e)) {
print('$url3: $e');
}
});
}