Skip to content

Commit eb749f0

Browse files
committed
update
1 parent fe881ae commit eb749f0

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

lib/common/config/config.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
class Config {
2+
3+
// Private constructor to prevent instantiation
4+
Config._();
5+
26
static bool? DEBUG = true;
37

48

lib/common/net/api.dart

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,24 @@ import 'package:gsy_github_app_flutter/common/net/result_data.dart';
1515
class HttpManager {
1616
static const CONTENT_TYPE_JSON = "application/json";
1717
static const CONTENT_TYPE_FORM = "application/x-www-form-urlencoded";
18-
19-
final Dio _dio = Dio(); // 使用默认配置
20-
21-
final TokenInterceptors _tokenInterceptors = TokenInterceptors();
22-
23-
HttpManager() {
24-
_dio.interceptors.add(HeaderInterceptors());
25-
26-
_dio.interceptors.add(_tokenInterceptors);
27-
28-
_dio.interceptors.add(LogsInterceptors());
29-
30-
_dio.interceptors.add(ErrorInterceptors());
31-
32-
_dio.interceptors.add(ResponseInterceptors());
18+
late final Dio _dio;
19+
late final TokenInterceptors _tokenInterceptors;
20+
21+
HttpManager._internal() {
22+
_dio = Dio(); // 使用默认配置
23+
_tokenInterceptors = TokenInterceptors();
24+
25+
_dio.interceptors.addAll([
26+
HeaderInterceptors(),
27+
_tokenInterceptors,
28+
LogsInterceptors(),
29+
ErrorInterceptors(),
30+
ResponseInterceptors(),
31+
]);
3332
}
3433

34+
static final HttpManager _instance = HttpManager._internal();
35+
3536
///发起网络请求
3637
///[ url] 请求url
3738
///[ params] 请求参数
@@ -91,6 +92,9 @@ class HttpManager {
9192
getAuthorization() async {
9293
return _tokenInterceptors.getAuthorization();
9394
}
95+
96+
/// 提供单例访问
97+
static HttpManager get instance => _instance;
9498
}
9599

96-
final HttpManager httpManager = HttpManager();
100+
final HttpManager httpManager = HttpManager.instance;

0 commit comments

Comments
 (0)