import 'dart:io'; import 'package:dio/dio.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter_easyloading/flutter_easyloading.dart'; import 'package:wow_english/network/basic_configuration.dart'; enum HttpMethod { get, put, post, head, patch, delete, } class DioUtil { static final Dio _dio = getDefDio(); static Dio getDefDio() { Dio dio = Dio(); dio.options = getDefOptions(); return dio; } static BaseOptions getDefOptions() { final BaseOptions options = BaseOptions(); options.baseUrl = BasicConfigurationManager().baseUrl??''; options.connectTimeout = const Duration(milliseconds: 1000); options.receiveTimeout = const Duration(milliseconds: 1000); options.headers['content-type'] = 'application/x-www-form-urlencoded'; return options; } Future requestData( String path, { data, HttpMethod method = HttpMethod.get, Map? queryParameters, ProgressCallback? onSendProgress, ProgressCallback? onReceiveProgress, required Function successCallBack, required Function errorCallBack, }) async{ try { Map headers = {}; if (method == HttpMethod.post) { headers['content-type'] = 'application/x-www-form-urlencoded'; } Response response; response = await _dio.request( path, data: data??{}, queryParameters: queryParameters, options: Options(method: method.name,headers: headers), onSendProgress: onSendProgress, onReceiveProgress: onReceiveProgress); if (response.statusCode == HttpStatus.ok || response.statusCode == HttpStatus.created) { successCallBack(response.data); } else { errorCallBack('请求失败'); } } on DioError catch(error) { EasyLoading.dismiss(); if (kDebugMode) { print(error); } rethrow; } } }