Comments (4)
反复测试发现是在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.
非常感谢你的反馈,但我这边并没有复现呢。
看了你的异常,应该是 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.
的确是这样的,我将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.
Hi, 0.2.1 版本应该就没问题拉。
from dio-http-cache.
Related Issues (20)
- if the internet is cut off and I display the data from the cache. I cannot manually delete
- Support on error callback
- Update the dependencies HOT 1
- Any example on cipher to encrypt cache? HOT 2
- at .wrapDatabaseException(.java:11)
- incompatible with newer dependencies HOT 2
- remove date when logout HOT 1
- Is this open source library?? HOT 1
- please add windows support HOT 1
- Json Serializable HOT 4
- Please add feature like getFileCached
- 建议
- Solving version fail HOT 6
- Change of copyright notice
- How I can disable this plugin for non Android / IOS / MACOS apps HOT 2
- [ Updated dependencies && Other pub.dev libs compatibility ] HOT 2
- Unhandled Exception: type 'Null' is not a subtype of type 'String' in type cast HOT 2
- Version Update HOT 6
- Updated the library to work with the latest DIO HOT 4
- clearAll() cache HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dio-http-cache.