request method
Future
request(
- String path, {
- String method = "GET",
- Map<
String, dynamic> ? params, - dynamic data,
- Options? options,
- CancelToken? cancelToken,
- bool refresh = false,
- bool noCache = true,
- String? cacheKey,
- bool needResponse = true,
- bool cacheDisk = false,
- bool isLoading = false,
- bool isCustomResponse = false,
- FailFunction? fail,
Implementation
Future request(String path,
{String method = "GET",
Map<String, dynamic>? params,
data,
Options? options,
CancelToken? cancelToken,
bool refresh = false,
bool noCache = true,
String? cacheKey,
bool needResponse = true,
bool cacheDisk = false,
bool isLoading = false,
bool isCustomResponse = false,
FailFunction? fail}) async {
if (isLoading) {
EasyLoading.show();
}
Options requestOptions = options ?? Options();
requestOptions = requestOptions.copyWith(
method: method,
extra: {
"refresh": refresh,
"noCache": noCache,
"cacheKey": cacheKey,
"cacheDisk": cacheDisk,
},
);
Response response;
CancelToken dioCancelToken = createDioCancelToken(cancelToken);
if (!path.startsWith("http")) {
var instance = Get.find<IDemonConfig>();
if (path.startsWith("/")) {
path = instance.getApiRoot() + path;
} else {
path = instance.getApiRoot() + "/" + path;
}
}
response = await dio
.request(
path,
data: data,
queryParameters: params,
options: requestOptions,
cancelToken: dioCancelToken,
)
.onError((error, stackTrace) {
if (fail == null) {
EasyLoading.showToast('网络错误');
} else {
fail(error, stackTrace);
}
return Future.error(() {});
}).whenComplete(() {
if (isLoading) {
EasyLoading.dismiss();
}
});
///debugPrint('Response==>:${response.data}');
pendingRequest.remove(dioCancelToken);
if (!needResponse) {
return "";
}
ApiResponse result;
if (response.data != null && !isCustomResponse) {
result = ApiResponse.fromJson(response.data);
} else {
return response.data;
}
if (result.code == 1) {
return result.data;
}
if (result.code == 401) {
final instance = Get.find<IDemonConfig>();
instance.makeLogout();
return Future.error(() {});
}
if (result.code != null) {
EasyLoading.showToast(result.message ?? "服务器繁忙");
return Future.error(() {});
}
return response.data;
}