flutter针对dio网络请求封装,get,post,动态添加headers

dio封装dart文件,包含get,post请求和自定义动态headers

import 'package:dio/dio.dart';
import 'dart:convert';
import 'package:micro_course/http/ResultCode.dart';
import 'package:micro_course/http/GlobalConfig.dart';
class DioManger{
  static DioManger _instance;

  static DioManger getInstance() {
    if (_instance == null) {
      _instance = new DioManger();
    }
    return _instance;
  }
  Dio dio = new Dio();

  DioManger(){
//    dio.options.headers = {
//    默认headers
//    };
    dio.options.baseUrl = "https://keep-portal.kuaizaixuetang.com/";
    dio.options.connectTimeout = 5000;
    dio.options.receiveTimeout = 3000;
  }

  get(String url,Map params,Map headers,Function successCallBack,Function errorCallBack) async{
   _requestHttp(url,headers,successCallBack,'get',params,errorCallBack);
  }

  post(String url,params,Map headers,Function successCallBack,Function errorCallBack) async{
   _requestHttp(url, headers,successCallBack,'post',params,errorCallBack);
  }

  _requestHttp(String url ,Map headers,Function successCallBack,[
    String method, Map params, Function errorCallBack
  ]) async{
    Response response;
    try{
      if(headers != null){
        ///显示指定Map的限定类型 动态添加headers
        dio.options.headers.addAll(new Map.from(headers));
      }
      if (method == 'get') {
        if (params != null && params.isNotEmpty) {
          response = await dio.get(url, queryParameters: params);
        } else {
          response = await dio.get(url);
        }
      } else if (method == 'post') {
        if (params != null && params.isNotEmpty) {
          response = await dio.post(url, data: params);
        } else {
          response = await dio.post(url);
        }
      }
    }on DioError catch(error){
      // 请求错误处理
      Response errorResponse;
      if (error.response != null) {
        errorResponse = error.response;
      } else {
        errorResponse = new Response(statusCode: 666);
      }
      // 请求超时
      if (error.type == DioErrorType.CONNECT_TIMEOUT) {
        errorResponse.statusCode = ResultCode.CONNECT_TIMEOUT;
      }
      // 一般服务器错误
      else if (error.type == DioErrorType.RECEIVE_TIMEOUT) {
        errorResponse.statusCode = ResultCode.RECEIVE_TIMEOUT;
      }
      // debug模式才打印
      if (GlobalConfig.isDebug) {
        print('请求异常: ' + error.toString());
        print('请求异常url: ' + url);
        print('请求头: ' + dio.options.headers.toString());
        print('method: ' + dio.options.method);
      }
      _error(errorCallBack, error.message);
      return '';
    }
    // debug模式打印相关数据
    if (GlobalConfig.isDebug) {
      print('请求url: ' + url);
      print('请求头: ' + dio.options.headers.toString());
      if (params != null) {
        print('请求参数: ' + params.toString());
      }
      if (response != null) {
        print('请求结果: ' + response.toString());
      }
    }
//    String dataStr = json.encode(response.data);
//    Map dataMap = json.decode(dataStr);
//    if (dataMap == null || dataMap['state'] == 0) {
//      _error(errorCallBack, '错误码:' + dataMap['errorCode'].toString() + ',' + response.data.toString());
//    }else if (successCallBack != null) {
//      successCallBack(dataMap);
//    }
    successCallBack(response.toString());

  }
  _error(Function errorCallBack, String error) {
    if (errorCallBack != null) {
      errorCallBack(error);
    }
  }
}

get请求使用:

  ///获取用户课程信息
  void getUserCourseList(LoginMsg loginMsg) {
    ///显示指定Map的限定类型
    Map parms = {"student_id":loginMsg.data.studentId.toString()};
    Map headers = {"token":loginMsg.data.loginToken.toString()};
    DioManger.getInstance().get(
        APIConfig.GET_COURSE_LIST,
        parms,
        headers,
        (data){
          print("获取课程列表成功:"+data.toString());
          /// 登录成功发送全局事件
            bus.emit('login',loginMsg);
        },
        (error){
          print("获取课程列表失败:"+error.toString());
        }
    );

  }

post请求示例:

 void login(Map params,BuildContext context) {
    DioManger.getInstance().post(
        APIConfig.LOGIN,
        params,null, (data){
          setState(() {
            print("登录成功:"+data.toString());
            Map jsonMsg = json.decode(data.toString());
            LoginMsg loginMsg = LoginMsg.fromJson(jsonMsg);
            print("json转实体类:"+loginMsg.toString());
            ///更新UI
//            Navigator.pushReplacement(context, MaterialPageRoute(
//                builder: (context) => BottomNavigationWidget(),
//                settings: RouteSettings(
//                arguments:jsonMsg,
//               ),
//            ));
           getUserCourseList(loginMsg);
          });

        }, (error){
          setState(() {

          });
          print("登录异常:"+error.toString());
        }
    );
  }

json解析错误处理

类似 type ‘_InternalLinkedHashMap’ is not a subtype of type ‘Map’ 这种常见异常是因为dynamic这个类型要明确一下,上述例子中此处的修改已经做了注释了,传餐时候使用Map这种明确泛型类型的就可以。

你可能感兴趣的:(Flutter)