onRequest method
Called when the request is about to be sent.
Implementation
@override
Future<void> onRequest(
RequestOptions options,
RequestInterceptorHandler handler,
) async {
try {
// 检查网络连接
final connectivity = await Connectivity().checkConnectivity();
if (connectivity == ConnectivityResult.none) {
// 无网络连接,拒绝请求
return handler.reject(
DioException(
requestOptions: options,
type: DioExceptionType.connectionError,
error: '网络异常,请检查网络连接',
),
);
} else {
// 有网络连接,继续请求
handler.next(options);
}
} catch (error) {
// 检查网络时出错,继续请求(保守策略)
handler.next(options);
}
}