DioUtils

一:初始化

在使用 DioUtils 之前,先进行基础配置的初始化:

DioConfigManager.instance.setBaseOptions(baseUrl: "https://www.google.com");
或者
DioConfigManager.instance.setBaseOptions(baseUrl: "https://www.google.com").setIsDefaultLog(true);

二:使用方式

Get请求

DioUtils.instance.get("/xxx/get").startRequest();

Get请求带参数

Map<String, dynamic> queryParams = {'key1': 'value1', 'key2': 'value2'};
DioUtils.instance.get("/xxx/get").queryParameters(queryParams).startRequest();

Post请求

DioUtils.instance.post("/xxx/post").startRequest();

Post请求带参数(JSON)

Map<String, dynamic> data = {'key1': 'value1', 'key2': 'value2'};
DioUtils.instance.post("/xxx/post").jsonData(data).startRequest();

POST请求带参数(表单格式)

Map<String, dynamic> formData = {'key1': 'value1', 'key2': 'value2'};
DioUtils.instance.post("/xxx/post").formUrlEncoded(formData).startRequest();

文件上传(单个或者多个)

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');
    },
);

暂停上传

    DioUtils.instance.pauseUpload(fileName);

继续上传

DioUtils.instance.resumeUpload(
    {'file': file, 'url': 'https://your.upload.url'},
    (fileName, sent, total) {
        print('Uploading $fileName: ${sent / total * 100}%');
    }
);

文件下载(单个或者多个)

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');
    },
);

暂停下载

    DioUtils.instance.pauseDownload(fileName);

继续下载

DioUtils.instance.resumeDownload(
    {'url': 'https://your.download.url/file', 'path': '/path/to/save/file'},
    (fileName, received, total) {
        print('Downloading $fileName: ${received / total * 100}%');
    }
);

设置单独请求头(单个)

DioUtils.instance
        .header("Authorization", "Bearer your_token")
        .get("/xxx/get")
        .startRequest();

设置单独请求头(多个)

DioUtils.instance
        .headers({"Authorization": "Bearer your_token", "Custom-Header": "value"})
        .get("/xxx/get")
        .startRequest();

设置统一请求头

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);
    }
}

设置连接超时和接收超时、

DioUtils.instance
        .connectTimeout(10) // 10秒
        .receiveTimeout(20) // 20秒
        .get("/xxx/get")
        .startRequest();

取消请求、

DioUtils.instance.cancelDio();

Libraries

dio_utils