1
1
import 'dart:io' ;
2
2
import 'package:dio/dio.dart' ;
3
3
4
- main () async {
4
+ void main () async {
5
5
var dio = Dio ();
6
6
dio.options
7
- ..baseUrl = " http://httpbin.org/"
7
+ ..baseUrl = ' http://httpbin.org/'
8
8
..connectTimeout = 5000 //5s
9
9
..receiveTimeout = 5000
10
- ..validateStatus = (int status) {
11
- return status > 0 ;
10
+ ..validateStatus = (int ? status) {
11
+ return status != null && status > 0 ;
12
12
}
13
13
..headers = {
14
14
HttpHeaders .userAgentHeader: 'dio' ,
15
15
'common-header' : 'xx' ,
16
16
};
17
17
18
- // Or you can create dio instance and config it as follow:
19
- // var dio = Dio(BaseOptions(
20
- // baseUrl: "http://www.dtworkroom.com/doris/1/2.0.0/",
21
- // connectTimeout: 5000,
22
- // receiveTimeout: 5000,
23
- // headers: {
24
- // HttpHeaders.userAgentHeader: 'dio',
25
- // 'common-header': 'xx',
26
- // },
27
- // ));
28
-
18
+ // Or you can create dio instance and config it as follow:
19
+ // var dio = Dio(BaseOptions(
20
+ // baseUrl: "http://www.dtworkroom.com/doris/1/2.0.0/",
21
+ // connectTimeout: 5000,
22
+ // receiveTimeout: 5000,
23
+ // headers: {
24
+ // HttpHeaders.userAgentHeader: 'dio',
25
+ // 'common-header': 'xx',
26
+ // },
27
+ // ));
29
28
dio.interceptors
30
29
..add (InterceptorsWrapper (
31
30
onRequest: (RequestOptions options) {
@@ -36,13 +35,13 @@ main() async {
36
35
))
37
36
..add (LogInterceptor (responseBody: false )); //Open log;
38
37
39
- Response response = await dio.get (" https://www.google.com/" );
38
+ var response = await dio.get (' https://www.google.com/' );
40
39
41
40
// Download a file
42
41
response = await dio.download (
43
- " https://www.google.com/" ,
44
- " ./example/xx.html" ,
45
- queryParameters: {"a" : 1 },
42
+ ' https://www.google.com/' ,
43
+ ' ./example/xx.html' ,
44
+ queryParameters: {'a' : 1 },
46
45
onReceiveProgress: (received, total) {
47
46
if (total != - 1 ) {
48
47
print ('$received ,$total ' );
@@ -51,24 +50,27 @@ main() async {
51
50
);
52
51
53
52
// Create a FormData
54
- FormData formData = FormData .fromMap ({
55
- " age" : 25 ,
56
- " file" : await MultipartFile .fromFile (
57
- " ./example/upload.txt" ,
58
- filename: " upload.txt" ,
53
+ var formData = FormData .fromMap ({
54
+ ' age' : 25 ,
55
+ ' file' : await MultipartFile .fromFile (
56
+ ' ./example/upload.txt' ,
57
+ filename: ' upload.txt' ,
59
58
)
60
59
});
61
60
62
61
// Send FormData
63
- response = await dio.post (" /test" , data: formData);
62
+ response = await dio.post (' /test' , data: formData);
64
63
print (response);
65
64
66
65
// post data with "application/x-www-form-urlencoded" format
67
66
response = await dio.post (
68
- " /test" ,
67
+ ' /test' ,
69
68
data: {
70
- "id" : 8 ,
71
- "info" : {"name" : "wendux" , "age" : 25 }
69
+ 'id' : 8 ,
70
+ 'info' : {
71
+ 'name' : 'wendux' ,
72
+ 'age' : 25 ,
73
+ }
72
74
},
73
75
options: Options (
74
76
contentType: Headers .formUrlEncodedContentType,
0 commit comments