/// 网络请求封装,单例模式
import 'package:dio/dio.dart';
class Fetch {
static final Fetch _fetch = Fetch._internal();
final Dio _dio = Dio(BaseOptions(baseUrl: 'http://xxx.xxx.xx.xx:xxxx'));
factory Fetch() => _fetch;
Fetch._internal();
Future<Response> get(String path, [Map<String, dynamic> query]) async {
Response response = Response(data: -1);
try {
response = await _dio.get(path, queryParameters: query);
} on DioError catch (e) {
print(e.error);
}
return response;
}
Future<Response> post(String path, [Map<String, dynamic> body]) async {
Response response = Response(data: -1);
try {
response = await _dio.post(
path,
data: body,
options: Options(
contentType: Headers.formUrlEncodedContentType,
),
);
} on DioError catch (e) {
print(e.error);
}
return response;
}
}
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
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