Giter Site home page Giter Site logo

Error while using about dio-http-cache HOT 4 CLOSED

hurshi avatar hurshi commented on July 19, 2024
Error while using

from dio-http-cache.

Comments (4)

BB-fat avatar BB-fat commented on July 19, 2024

反复测试发现是在app启动后一段时间这部分异常,加了个try之后可以正常拿到body,header索性忽略了
Response _buildResponse(String data, RequestOptions options) {
var headers = Headers();
// 由于header这里在app启动的时候总抛出异常,所以加一层try忽略
try {
options.headers.forEach((k, v) => headers.add(k, v));
}catch(e){}
return Response(
data: (options.responseType == ResponseType.json)
? jsonDecode(data)
: data,
headers: headers,
extra: options.extra..remove(DIO_CACHE_KEY_MAX_AGE),
statusCode: 200);
}

from dio-http-cache.

hurshi avatar hurshi commented on July 19, 2024

非常感谢你的反馈,但我这边并没有复现呢。
看了你的异常,应该是 header 中有一个 key 为 null, 而导致 crash。
不知你能提供下异常的环境不?方便的话,可以贴一下你请求的数据,比如:

var _dio = Dio(
  BaseOptions(
    baseUrl: "https://www.wanandroid.com/", 
    contentType: "application/x-www-form-urlencoded; charset=utf-8")
  )
  ..interceptors.add(_manager.interceptor)
  ..interceptors.add(LogInterceptor(responseBody: true));

void rq() {
  _dio.post(
      "www.baidu.com", 
      data: {'k': "keyword"}, 
      options: buildCacheOptions(Duration(hours: 1)))
  .then((response) {
      print(">>> ${response.data}");
  });
}

或者提供一个能重现 crash 的 demo 就更好了。

from dio-http-cache.

BB-fat avatar BB-fat commented on July 19, 2024

的确是这样的,我将dio封装了一下,一开始token默认为null然后返回认证失败的时候再请求token

//  刷新token
  Future _refreshToken() async {
    if (Global.debug) {
      token = "debug";
      return;
    }
    Dio d = Dio();
    d.interceptors.add(LogInterceptor(responseBody: false));
    Response res = await d.post(_baseurl + "/api/v1/auth/token",
        data: json.encode({"id": Global.id, "message": Global.aesKey}));
    if (res.statusCode == 403) {
      Global.id = Global.aesKey = null;
//      TODO 需要登陆时跳转到登陆界面
//      Navigator.popUntil(context, predicate)
    }
    token = res.data["token"];
  }

  Future get(String url,
      {Map<String, dynamic> params = null,
      Duration cacheTime = const Duration(days: 5),
      bool useCache = true}) async {
    Response res = await _dio.get(
      _baseurl + url,
      queryParameters: params,
      options: buildCacheOptions(cacheTime,
          forceRefresh: !useCache, options: Options(headers: {"Token": token})),
    );
    if (res.statusCode == 401) {
      await _refreshToken();
      return get(url, params: params, cacheTime: cacheTime, useCache: useCache);
    } else {
      return res;
    }
  }

所以app的第一次请求header里的token是null。

from dio-http-cache.

hurshi avatar hurshi commented on July 19, 2024

Hi, 0.2.1 版本应该就没问题拉。

from dio-http-cache.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.