Flutter——Dio网络库的使用与封装

导入依赖

 #网络库
  dio: ^4.0.0

定义变量

// default options
  static const int _connectTimeout = 15000; //15s
  static const int _receiveTimeout = 15000;
  static const int _sendTimeout = 10000;

  static const String GET = 'get';
  static const String POST = 'post';
  static const String PUT = 'put';
  static const String PATCH = 'patch';
  static const String DELETE = 'delete';
  
  static var _dio;
  static HttpUtil? _httpUtil;
  
  static HttpUtil getInstance() => _httpUtil ??= HttpUtil();

创建Dio对象

 // 创建 dio 实例对象
   Dio _createInstance() {
    if (_dio == null) {
      /// 全局属性:请求前缀、连接超时时间、响应超时时间
      var options = BaseOptions(
        // responseType: ResponseType.json,
        baseUrl: HttpPath.BaseUrl,
        connectTimeout: _connectTimeout,
        receiveTimeout: _receiveTimeout,
        sendTimeout: _sendTimeout,
      );

      //拦截器
      var interceptor = InterceptorsWrapper(
        onRequest: (options,handler){
          print("\n================================= 请求数据 =================================");
          print("method = ${options.method.toString()}");
          print("url = ${options.uri.toString()}");
          print("headers = ${options.headers}");
          print("params = ${options.queryParameters}");
          return handler.next(options);
        },
        onResponse: (response,handler,){
          print("\n================================= 响应数据开始 =================================");
          print("code = ${response.statusCode}");
          print("data = ${response.data}");
          print("================================= 响应数据结束 =================================\n");
          return handler.next(response);
        },
        onError: (e,handler){
          print("\n=================================错误响应数据 =================================");
          print("type = ${e.type}");
          print("message = ${e.message}");
          print("stackTrace = ${e.stackTrace}");
          print("\n");
          return handler.next(e);
        }
      );

      _dio = Dio(options);
      _dio.interceptors.add(interceptor);
    }
    return _dio;
  }

封装Get和Post请求

Future<T> get<T>(String url, FormData? param) async {
    return _requestHttp<T>(
      url,
      param: param,
      method: GET,
    );
  }

  Future<T> post<T>(String url, FormData param) async {
    return _requestHttp<T>(
      url,
      param: param,
      method: POST,
    );
  }

网络请求

_requestHttp<T>(String url, {param, method}) async {
    _dio = _createInstance();
    try {
      Response response = await _dio.request(
          url,
          data: param,
          options: Options(method: method));
      if (response.statusCode == 200) return response.data;

    } on DioError catch (e) {
      /// 打印请求失败相关信息
      print("【请求出错】${e.toString()}");
      rethrow;
    }
  }

释放dio对象

clear() {
    _dio = null;
  }

封装类

class HttpUtil {
  // default options
  static const int _connectTimeout = 15000; //15s
  static const int _receiveTimeout = 15000;
  static const int _sendTimeout = 10000;

  static const String GET = 'get';
  static const String POST = 'post';
  static const String PUT = 'put';
  static const String PATCH = 'patch';
  static const String DELETE = 'delete';

  static var _dio;
  static HttpUtil? _httpUtil;

  static HttpUtil getInstance() => _httpUtil ??= HttpUtil();

  // 创建 dio 实例对象
   Dio _createInstance() {
    if (_dio == null) {
      /// 全局属性:请求前缀、连接超时时间、响应超时时间
      var options = BaseOptions(
        // responseType: ResponseType.json,
        baseUrl: HttpPath.BaseUrl,
        connectTimeout: _connectTimeout,
        receiveTimeout: _receiveTimeout,
        sendTimeout: _sendTimeout,
      );

      //拦截器
      var interceptor = InterceptorsWrapper(
        onRequest: (options,handler){
          print("\n================================= 请求数据 =================================");
          print("method = ${options.method.toString()}");
          print("url = ${options.uri.toString()}");
          print("headers = ${options.headers}");
          print("params = ${options.queryParameters}");
          return handler.next(options);
        },
        onResponse: (response,handler,){
          print("\n================================= 响应数据开始 =================================");
          print("code = ${response.statusCode}");
          print("data = ${response.data}");
          print("================================= 响应数据结束 =================================\n");
          return handler.next(response);
        },
        onError: (e,handler){
          print("\n=================================错误响应数据 =================================");
          print("type = ${e.type}");
          print("message = ${e.message}");
          print("stackTrace = ${e.stackTrace}");
          print("\n");
          return handler.next(e);
        }
      );

      _dio = Dio(options);
      _dio.interceptors.add(interceptor);
    }
    return _dio;
  }


  Future<T> get<T>(String url, FormData? param) async {
    return _requestHttp<T>(
      url,
      param: param,
      method: GET,
    );
  }

  Future<T> post<T>(String url, FormData param) async {
    return _requestHttp<T>(
      url,
      param: param,
      method: POST,
    );
  }

    /// T is Map<String,dynamic>  or List<dynamic>
    _requestHttp<T>(String url, {param, method}) async {
    _dio = _createInstance();
    try {
      Response response = await _dio.request(
          url,
          data: param,
          options: Options(method: method));
      if (response.statusCode == 200) return response.data;

    } on DioError catch (e) {
      /// 打印请求失败相关信息
      print("【请求出错】${e.toString()}");
      rethrow;
    }
  }

  // 清空 dio 对象
  clear() {
    _dio = null;
  }

}

解析

使用一个插件JsonToDartBeanAction,建造一个新的实体类,只需将对应的Json字符串复制进去,然后它就会自动生成实体类和对应的解析类

使用

Future<List<ITopEntity>?> getTopList(int skip,int limit) async {
    var url = HttpPath.getTop250(skip, limit);
    final result = await HttpUtil.getInstance().get(url, null);
    return jsonConvert.convertListNotNull<ITopEntity>(result);
}

猜你喜欢

转载自blog.csdn.net/News53231323/article/details/128371501