flutter 2.0 升級(jí)至 dio 3.0升級(jí)到4.0 遇到的問(wèn)題

首先會(huì)出現(xiàn)這個(gè)錯(cuò)誤,經(jīng)過(guò)百度查詢很多文章都是說(shuō)的網(wǎng)絡(luò)權(quán)限的問(wèn)題,在排查不是權(quán)限的問(wèn)題是經(jīng)過(guò)一番折騰后終于解決了問(wèn)題。

OS Error: No address associated with hostname, errno = 7

以下代碼在dio3.0的時(shí)候不會(huì)出現(xiàn)問(wèn)題,但是4.0的時(shí)候會(huì)報(bào)錯(cuò)

dio.interceptors.add(InterceptorsWrapper(
      onRequest:(RequestOptions options, RequestInterceptorHandler type){
        Map<String, dynamic> headers = options.headers;
        String tokenHead = SpUtil.getString("tokenHead");
        String toKen = SpUtil.getString("token");
        if(toKen!=null && options.path.indexOf('api/v1/login')==-1 && options.path.indexOf('loginByWeChat')==-1 && options.path.indexOf('aliyuncs.com')==-1 && options.path.indexOf('weixin.qq.com')==-1){
          headers[tokenHead] = toKen;
        }
        // 在請(qǐng)求被發(fā)送之前做一些事情
        print('請(qǐng)求頭部:${options.headers}');
        return options;
      },
      onResponse:(Response response, ResponseInterceptorHandler type) {
        // 在返回響應(yīng)數(shù)據(jù)之前做一些預(yù)處理
        return response;
      },
      onError: (DioError e, ErrorInterceptorHandler type) {
        // 當(dāng)請(qǐng)求失敗時(shí)做一些預(yù)處理
        return e;
      }
    ));

修改后的代碼

dio.interceptors.add(InterceptorsWrapper(
      onRequest:(RequestOptions options, RequestInterceptorHandler handler){
        Map<String, dynamic> headers = options.headers;
        String tokenHead = SpUtil.getString("tokenHead");
        String toKen = SpUtil.getString("token");
        if(toKen!=null && options.path.indexOf('api/v1/login')==-1 && options.path.indexOf('loginByWeChat')==-1 && options.path.indexOf('aliyuncs.com')==-1 && options.path.indexOf('weixin.qq.com')==-1){
          headers[tokenHead] = toKen;
        }
        // 在請(qǐng)求被發(fā)送之前做一些事情
        handler.next(options);
      },
      onResponse:(Response response, ResponseInterceptorHandler handler) {
        // 在返回響應(yīng)數(shù)據(jù)之前做一些預(yù)處理
        handler.next(response);
      },
      onError: (DioError e, ErrorInterceptorHandler handler) {
        // 當(dāng)請(qǐng)求失敗時(shí)做一些預(yù)處理
        handler.next(e);
      }
    ));
  }

希望以上代碼可以給你幫助,也警醒自己在日常編程的過(guò)程中加強(qiáng)嚴(yán)謹(jǐn)性。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容