[Flutter3] 记录Dio的简单封装
import 'package:dio/dio.dart';
class DioUtil {
static Dio dio = Dio();
// 发送GET请求
static Future<Response> get(String url, {Map<String, dynamic>? queryParameters, Options? options}) async {
Response response;
try {
response = await dio.get(url, queryParameters: queryParameters, options: options);
print('请求成功: $response');
} on DioError catch (e) {
print('请求失败: ${e.message}');
}
return response!;
}
// 发送POST请求
static Future<Response> post(String url, {data, Options? options}) async {
Response response;
try {
response = await dio.post(url, data: data, options: options);
print('请求成功: $response');
} on DioError catch (e) {
print('请求失败: ${e.message}');
}
return response!;
}
}
这段代码提供了一个简单的Dio HTTP客户端封装,包括GET和POST请求的基本实现。它使用Dio库发送HTTP请求,并打印出请求结果。这是一个开发者在使用Flutter进行网络请求时可以参考和学习的示例。
评论已关闭