0 purchases
dio utils x
DioUtils #
一:初始化 #
在使用 DioUtils 之前,先进行基础配置的初始化:
DioConfigManager.instance.setBaseOptions(baseUrl: "https://www.google.com");
或者
DioConfigManager.instance.setBaseOptions(baseUrl: "https://www.google.com").setIsDefaultLog(true);
copied to clipboard
二:使用方式 #
Get请求 #
DioUtils.instance.get("/xxx/get").startRequest();
copied to clipboard
Get请求带参数 #
Map<String, dynamic> queryParams = {'key1': 'value1', 'key2': 'value2'};
DioUtils.instance.get("/xxx/get").queryParameters(queryParams).startRequest();
copied to clipboard
Post请求 #
DioUtils.instance.post("/xxx/post").startRequest();
copied to clipboard
Post请求带参数(JSON) #
Map<String, dynamic> data = {'key1': 'value1', 'key2': 'value2'};
DioUtils.instance.post("/xxx/post").jsonData(data).startRequest();
copied to clipboard
POST请求带参数(表单格式) #
Map<String, dynamic> formData = {'key1': 'value1', 'key2': 'value2'};
DioUtils.instance.post("/xxx/post").formUrlEncoded(formData).startRequest();
copied to clipboard
文件上传(单个或者多个) #
List<Map<String, dynamic>> files = [
{'file': File('/path/to/file1'), 'url': 'https://your.upload.url1'},
{'file': File('/path/to/file2'), 'url': 'https://your.upload.url2'}
];
DioUtils.instance.uploadFiles(
files,
onSendProgress: (fileName, sent, total) {
print('Uploading $fileName: ${sent / total * 100}%');
},
onPauseResume: (fileName, isPaused) {
print(isPaused ? '$fileName upload paused' : '$fileName upload resumed');
},
);
copied to clipboard
暂停上传 #
DioUtils.instance.pauseUpload(fileName);
copied to clipboard
继续上传 #
DioUtils.instance.resumeUpload(
{'file': file, 'url': 'https://your.upload.url'},
(fileName, sent, total) {
print('Uploading $fileName: ${sent / total * 100}%');
}
);
copied to clipboard
文件下载(单个或者多个) #
List<Map<String, dynamic>> files = [
{'url': 'https://your.download.url/file1', 'path': '/path/to/save/file1'},
{'url': 'https://your.download.url/file2', 'path': '/path/to/save/file2'}
];
DioUtils.instance.downloadFiles(
files,
onReceiveProgress: (fileName, received, total) {
print('Downloading $fileName: ${received / total * 100}%');
},
onPauseResume: (fileName, isPaused) {
print(isPaused ? '$fileName download paused' : '$fileName download resumed');
},
);
copied to clipboard
暂停下载 #
DioUtils.instance.pauseDownload(fileName);
copied to clipboard
继续下载 #
DioUtils.instance.resumeDownload(
{'url': 'https://your.download.url/file', 'path': '/path/to/save/file'},
(fileName, received, total) {
print('Downloading $fileName: ${received / total * 100}%');
}
);
copied to clipboard
设置单独请求头(单个) #
DioUtils.instance
.header("Authorization", "Bearer your_token")
.get("/xxx/get")
.startRequest();
copied to clipboard
设置单独请求头(多个) #
DioUtils.instance
.headers({"Authorization": "Bearer your_token", "Custom-Header": "value"})
.get("/xxx/get")
.startRequest();
copied to clipboard
设置统一请求头 #
1:创建一个Dart文件
2:xxx_header.dart
3:
class XxxHeaderInterceptors extends Interceptor {
@override
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
options.headers["Authorization"] = token;
handler.next(options);
}
}
copied to clipboard
设置连接超时和接收超时、 #
DioUtils.instance
.connectTimeout(10) // 10秒
.receiveTimeout(20) // 20秒
.get("/xxx/get")
.startRequest();
copied to clipboard
取消请求、 #
DioUtils.instance.cancelDio();
copied to clipboard
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.