加入依赖:dio: ^3.0.9
class DioUtils {
/// global dio object
static Dio dio;
/// default options
static const int CONNECT_TIMEOUT = 10000;
static const int RECEIVE_TIMEOUT = 3000;
/// http request methods
static const String GET = 'get';
static const String NEWGET = 'newGet';
static const String POST = 'post';
static const String PUT = 'put';
static const String PATCH = 'patch';
static const String DELETE = 'delete';
/// 创建 dio 实例对象
static Future<Dio> createInstance() async {
if (dio == null) {
/// 全局属性:请求前缀、连接超时时间、响应超时时间
var options = BaseOptions(
connectTimeout: 350000,
receiveTimeout: 150000,
responseType: ResponseType.json,
validateStatus: (status) {
// 不使用http状态码判断状态,使用AdapterInterceptor来处理(适用于标准REST风格)
return true;
},
baseUrl: baseURL,
headers: {
'Accept': 'application/json,*/*',
'Content-Type': 'application/json',
//'token':''
});
dio = new Dio(options);
dio.interceptors.add(InterceptorsWrapper(
onRequest: (RequestOptions options) {
/*. 拦截器请求 时 相关处理*/
print('上传接口:${options.uri}');
},
onResponse: (e) {
/* 拦截器 请求到数据后的 相关处理*/
print('获取数据信息:$e');
},
onError: (e) {
Navigator.pop(Router.navigatorState.currentState.context);
_showErrorDialog('网络连接错误,请重试!');
print('拦截器错误信息-->:$e');
},
));
}
return dio;
}
/// 清空 dio 对象
static clear() {
dio = null;
}
///Get请求
static void getHttp<T>(
String url, {
parameters,
Function(T) onSuccess,
Function(String error) onError,
}) async {
try {
Dio dios = await createInstance();
Response response;
response = await dios.get(
url,
queryParameters: parameters,
); //cancelToken: token
var responseData = response.data;
var i = responseData['msg'];
if (responseData['code'] == 0) {
onSuccess(responseData['data']);
} else {
onError(responseData['msg']);
}
print('get返回结果-->$responseData');
print('get返回结果$i');
} catch (e) {
print('请求出错:' + e.toString());
onError(e.toString());
}
}
///Post请求
static void postHttp<T>(
String url, {
parameters,
Function(T) onSuccess,
Function(String error) onError,
}) async {
try {
Dio dios = await createInstance();
Response response = await dios.post(
url,
data: parameters,
); //cancelToken: token
var responseData = response.data;
var i = responseData['msg'];
if (responseData['code'] == 0) {
onSuccess(responseData['data']);
} else if (responseData['code'] == 200) {
print('返回结果-->${responseData['code']}');
onSuccess(responseData['data']);
} else {
print('指向这里');
onError(responseData['msg']);
}
print('post返回结果$i');
} catch (e) {
print('post返回结果x $e');
}
}
/// request Get、Post 请求
//url 请求链接
//parameters 请求参数
//method 请求方式
//onSuccess 成功回调
//onError 失败回调
static void requestHttp<T>(String url,
{parameters,
method,
Function(T t) onSuccess,
Function(String error) onError}) async {
parameters = parameters ?? {};
method = method ?? 'GET';
if (method == DioUtils.GET) {
getHttp(
url,
parameters: parameters,
onSuccess: (data) {
onSuccess(data);
},
onError: (error) {
onError(error);
},
);
} else if (method == DioUtils.NEWGET) {
newGetHttp(
url,
parameters: parameters,
onSuccess: (data) {
onSuccess(data);
},
onError: (error) {
onError(error);
},
);
} else if (method == DioUtils.POST) {
postHttp(
url,
parameters: parameters,
onSuccess: (data) {
onSuccess(data);
},
onError: (error) {
onError(error);
},
);
} else if (method == DioUtils.PUT) {
putHttp(
url,
parameters: parameters,
onSuccess: (data) {
onSuccess(data);
},
onError: (error) {
onError(error);
},
);
} else if (method == DioUtils.DELETE) {
deleteHttp(
url,
parameters: parameters,
onSuccess: (data) {
onSuccess(data);
},
onError: (error) {
onError(error);
},
);
}
}
只是简单的封装了Dio请求,具体可以根据自身需要做进一步处理!!!
欢迎加QQ群讨论:531134701