当前位置:   article > 正文

flutter项目实战三:封装http工具类_dart http插件封装

dart http插件封装

在这里插入图片描述

创建http开发目录

Api.dart为接口地址
custom_Interceptors.dart 为拦截器
http_utils.dart 为实际的http工具类

  • 统一header
  • sendTimeout 发送超时
  • receiveTimeout 接收超时
  • get请求方式
  • post请求凡是
  • form表单请求方式
import 'dart:io';

import 'package:dio/dio.dart';
import 'package:mianzu_null_safety/http/Api.dart';

import 'custom_Interceptors.dart';
late Dio dio;
class HttpUtil{
  static HttpUtil get instance=>_getInstance();
  static HttpUtil? _httpUtil;
  static HttpUtil _getInstance(){
    return _httpUtil??HttpUtil();
  }
  getHeader(){
    return {
      'Accept': 'application/json, text/plain, */*',
      'Content-Type': 'multipart/form-data',
      'Authorization': "*",
      'User-Aagent': "4.1.0;android;6.0.1;default;A001",
      "HZUID": "2",
      "Access-Control-Allow-Origin": "*",
      "Access-Control-Allow-Headers":true
    };
  }

  HttpUtil(){
    dio = Dio();
    dio.options..baseUrl = Api.BASE_URL
    ..sendTimeout=5000
    ..receiveTimeout=5000
    ..validateStatus = (int ? status){
      return status!=null&&status>0;
    }
    ..headers=getHeader();

    dio.interceptors
      // ..add(LogInterceptor())
      ..add(CustomInterceptors());
  }

  Future get(String url,{Map<String,dynamic>? parameters,Options? options}) async{
    Response response;
    if(parameters!=null&&options!=null){
      response = await dio.get(url,queryParameters: parameters,options: options);
    }else if (parameters != null && options == null) {
      response = await dio.get(url, queryParameters: parameters);
    } else if (parameters == null && options != null) {
      response = await dio.get(url, options: options);
    } else {
      response = await dio.get(url);
    }
    return response;
  }
  ///post
  Future post(String url, {required Map<String, dynamic>? parameters, Options? options}) async {
    Response response;
    if (parameters != null && options != null) {
      response = await dio.post(url, data: parameters, options: options);
    } else if (parameters != null && options == null) {
      response = await dio.post(url, data: parameters);
    } else if (parameters == null && options != null) {
      response = await dio.post(url, options: options);
    } else {
      response = await dio.post(url);
    }
    return response;
  }
  ///表单请求
  Future postFormData(String url, {required FormData? parameters, Options? options}) async {
    Response response;
    if (parameters != null && options != null) {
      response = await dio.post(url, data: parameters, options: options);
    } else if (parameters != null && options == null) {
      response = await dio.post(url, data: parameters);
    } else if (parameters == null && options != null) {
      response = await dio.post(url, options: options);
    } else {
      response = await dio.post(url);
    }
    return response;
  }
}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
拦截器
  • 在http_utils 中添加 (可在拦截器中动态配置header信息)
import 'package:dio/dio.dart';
import 'package:mianzu_null_safety/constant/constant.dart';
import 'package:mianzu_null_safety/utils/log_utils.dart';
import 'package:mianzu_null_safety/utils/share_preference_utils.dart';
class CustomInterceptors extends Interceptor {
  @override
  void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
    SharePreferenceUtils.getShareData(Constant.username).then((value) => {
      options.headers.addAll({"authorization":value??""}),
    });
    LogUtils.logInfo('CustomInterceptors onRequest PATH:=[${options.path}]\nHEADERS:=[${options.headers}]\nREQUEST:=[${options.method}]');
    return super.onRequest(options, handler);
  }
  @override
  void onResponse(Response response, ResponseInterceptorHandler handler) {
    LogUtils.logInfo('CustomInterceptors onResponse PATH:=[${response.requestOptions.path}]\n=> statusCode:=[${response.statusCode}]\n=>data:=[${response.data}]');
    super.onResponse(response, handler);
  }
  @override
  // Future onError(DioError err, ErrorInterceptorHandler handler) {
  void onError(DioError err, ErrorInterceptorHandler handler) {
    LogUtils.logInfo('CustomInterceptors ERROR[${err.response?.statusCode}]\nPATH: ${err.requestOptions.path}');
    return handler.next(err);
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

Api统一配置

class Api{
  static const String IMG_URL = 'https://mianzu.eatandshow.com/';
  static const String BASE_URL = 'https://mianzu.eatandshow.com';
  static const String REGISTER_URL=BASE_URL+'/admin.php/Admin/register';//用户注册
  static const String LOGIN_URL=BASE_URL+'/admin.php/Admin/tologin';//用户登录
  static const String UPDATEUSER=BASE_URL+'/admin.php/Admin/updateuser';//更新用户信息
  static const String GETUSERINFO=BASE_URL+'/admin.php/Admin/getuserinfo';//获取用户信息
  static const String SENDHOUSE=BASE_URL+'/admin.php/House/sendHouse';//房东发布house
  static const String FINDHOUSE=BASE_URL+'/admin.php/House/findhouse';//查询所有house列表
  static const String HOTHOUSE=BASE_URL+'/admin.php/House/hothouse';//热度值最高前三house
  static const String HOUSEDETAIL=BASE_URL+'/admin.php/House/houseDetail';//房间招租详情
  static const String INTENTIONHOUSE=BASE_URL+'/admin.php/Rent_House/intentionhouse';//租客意向房源
  static const String FIND_LIKE_HOUSE=BASE_URL+'/admin.php/Rent_House/find_like_house';//喜欢房源
  static const String SENDRENTHOUSE=BASE_URL+'/admin.php/Rent_House/sendRentHouse';//租客发布求租意向
  static const String FIND_RENT_HOUSE=BASE_URL+'/admin.php/Rent_House/find_rent_house';//查询租客发布
  static const String TOP_THREE_HOT_RENT_HOUSE=BASE_URL+'/admin.php/Rent_House/top_three_hot_rent_house';//查询租客发布前三热门
  static const String RENT_HOUSEDETAIL=BASE_URL+'/admin.php/Rent_House/rent_house_detail';//查询租客意向单详情
  static const String MAINCONTENT=BASE_URL+'/admin.php/Main_Home/mainContent';//首页banner+icon
  static const String MYHOUSE=BASE_URL+'/admin.php/House/myHouse';//我的房源
  static const String LIKERENTUSER=BASE_URL+'/admin.php/Rent_House/likeRentUser';//是否喜欢求租者
  static const String ALL_SKILL=BASE_URL+'/admin.php/Skill/skill';//所有租房技能标签
  static const String FEEDBACK=BASE_URL+'/admin.php/Feed_Back/feedback';//反馈
  static const String CHECK_UPDATE=BASE_URL+'/admin.php/Main_Home/check_update';//app检查更新
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/521627
推荐阅读
相关标签
  

闽ICP备14008679号