Flutter Dio封装与使用

依赖库版本

dio: 2.0.14

1.简单封装

import 'package:dio/dio.dart';
import 'dart:async';

/*
 * 封装 restful 请求
 *
 * GET、POST、DELETE、PATCH
 * 主要作用为统一处理相关事务:
 *  - 统一处理请求前缀;
 *  - 统一打印请求信息;
 *  - 统一打印响应信息;
 *  - 统一打印报错信息;
 */
class DioUtils {
  /// global dio object
  static Dio dio;

  /// default options
  static const String API_PREFIX = 'https://novel.dkvirus.com/api/v1';
  static const int CONNECT_TIMEOUT = 10000;
  static const int RECEIVE_TIMEOUT = 3000;

  /// http request methods
  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';

  ///Get请求测试
  static void getHttp() async {
    try {
      Response response = await Dio().get("http://www.google.cn");
      print("response$response");
    } catch (e) {
      print(e);
    }
  }

  ///Post请求测试
  static void postHttp(
    String url, {
    parameters,
    Function(T t) onSuccess,
    Function(String error) onError,
  }) async {

    ///定义请求参数
    parameters = parameters ?? {};
    //参数处理
    parameters.forEach((key, value) {
      if (url.indexOf(key) != -1) {
        url = url.replaceAll(':$key', value.toString());
      }
    });

    try {
      Response response;
      Dio dio = createInstance();
      response = await dio.post(url, data: parameters);
      if (response.statusCode == 200) {
        if (onSuccess != null) {
          onSuccess(response.data);
        }
      } else {
        throw Exception('statusCode:${response.statusCode}');
      }
      print('响应数据:' + response.toString());
    } catch (e) {
      print('请求出错:' + e.toString());
      onError(e.toString());
    }
  }

  /// request method
  //url 请求链接
  //parameters 请求参数
  //metthod 请求方式
  //onSuccess 成功回调
  //onError 失败回调
  static Future request(String url,
      {parameters,
      method,
      Function(T t) onSuccess,
      Function(String error) onError}) async {
    parameters = parameters ?? {};
    method = method ?? 'GET';

    /// 请求处理
    parameters.forEach((key, value) {
      if (url.indexOf(key) != -1) {
        url = url.replaceAll(':$key', value.toString());
      }
    });

    /// 打印:请求地址-请求方式-请求参数
    print('请求地址:【' + method + '  ' + url + '】');
    print('请求参数:' + parameters.toString());

    Dio dio = createInstance();
    //请求结果
    var result;
    try {
      Response response = await dio.request(url,
          data: parameters, options: new Options(method: method));
      result = response.data;
      if (response.statusCode == 200) {
        if (onSuccess != null) {
          onSuccess(result);
        }
      } else {
        throw Exception('statusCode:${response.statusCode}');
      }
      print('响应数据:' + response.toString());
    } on DioError catch (e) {
      print('请求出错:' + e.toString());
      onError(e.toString());
    }

    return result;
  }

  /// 创建 dio 实例对象
  static Dio createInstance() {
    if (dio == null) {
      /// 全局属性:请求前缀、连接超时时间、响应超时时间
      var options = BaseOptions(
        connectTimeout: 15000,
        receiveTimeout: 15000,
        responseType: ResponseType.plain,
        validateStatus: (status) {
          // 不使用http状态码判断状态,使用AdapterInterceptor来处理(适用于标准REST风格)
          return true;
        },
        baseUrl: "http://poetry.huhustory.com/",
      );

      dio = new Dio(options);
    }

    return dio;
  }

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

2.使用

 @override
  Future initState() {
    // TODO: implement initState
    super.initState();
    FormData formData = new FormData.from({
      "uuid": "9dad1c81889ff0ea8c8c5ade6a5e36e3",
      "sign": "",
      "sso_id": "",
      "own_version": "100",
      "channel": "ios",
      "dev_os_version": "iOS12.3.1",
      "timestamp": "1564974676",
      "screen_size": "[375,667]",
      "is_jailbreak": 0,
      "os": "ios",
      "device_name": "iPhone6S",
      "os_version": "",
      "country_code": "CN",
      "ios_version": "1.0.0",
    });
    DioUtils.postHttp(
      "huhuapi/firstnew/indexnew.html",
      parameters: formData,
      onSuccess: (data) {
        Toast.show('来了');
      },
      onError: (error) {
        Toast.show(error);
      },
    );
  }

未完,待续~

你可能感兴趣的:(Flutter Dio封装与使用)