Reputation: 39095
Now I upgrade dio to 4.0.0 that supports null safety:
dio: 4.0.0
http: 0.13.1
and tweak the http interceptor code like this:
class AppInterceptors extends InterceptorsWrapper {
@override
Future onRequest(RequestOptions options, RequestInterceptorHandler handler) async {
try {
if (!options.headers.containsKey("token")) {
String? token = await storage.read(key: "token");
options.headers["token"] = token;
return options;
}
} on Exception catch (e) {
CruiseLogHandler.logErrorException('interceptor error', e);
}
return super.onRequest(options, handler);
}
}
when the code run to options.headers["token"] = token;
, it break and no error output, also not send any http request to the server side, I did not figure out what's going on. what should I do to fix it?(dio version 3.x works fine) this is the flutter doctor output:
$ ~/apps/flutter/bin/flutter doctor ‹ruby-2.7.2›
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 2.0.3, on Mac OS X 10.15.7 19H114 darwin-x64, locale en-CN)
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
[!] Xcode - develop for iOS and macOS
✗ CocoaPods installed but not working.
You appear to have CocoaPods installed but it is not working.
This can happen if the version of Ruby that CocoaPods was installed with is different from the one being used to invoke it.
This can usually be fixed by re-installing CocoaPods.
To re-install see https://guides.cocoapods.org/using/getting-started.html#installation for instructions.
[✓] Chrome - develop for the web
[✓] Android Studio (version 4.1)
[✓] IntelliJ IDEA Community Edition (version 2020.3.3)
[✓] IntelliJ IDEA Ultimate Edition (version 2019.2.2)
[✓] VS Code (version 1.54.3)
[✓] Connected device (3 available)
! Error: xiaoqiang 的 iPhone is not connected. Xcode will continue when xiaoqiang 的 iPhone is connected. (code -13)
! Doctor found issues in 1 category.
(base)
I have tried write like this:
options.headers = {
'token': token,
...options.headers
};
still not work. Today I removed onRequest
code:
try {
if (!options.headers.containsKey("token")) {
String? token = await storage.read(key: "token");
/*options.headers = {
'token': token,
...options.headers
};*/
return options;
}
} on Exception catch (e) {
CruiseLogHandler.logErrorException('interceptor error', e);
}
and it could send http request. It seems could not edit RequestOptions before send http request in dio version 4.0.0 . Any one encount the same problem?
Upvotes: 3
Views: 1273
Reputation: 6353
In the new version they've updated the way interceptors work. They now return void
and in order to proceed you have to use handler
, so update you interceptor:
class AppInterceptors extends InterceptorsWrapper {
@override
Future<void> onRequest(RequestOptions options, RequestInterceptorHandler handler) async {
try {
if (!options.headers.containsKey("token")) {
String? token = await storage.read(key: "token");
options.headers["token"] = token;
}
} on Exception catch (e) {
CruiseLogHandler.logErrorException('interceptor error', e);
}
handler.next(options);
}
}
Upvotes: 9