Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DioError [DioErrorType.DEFAULT]: type 'int' is not a subtype of type 'String' of 'value' #186

Closed
joker-fu opened this issue Feb 22, 2019 · 3 comments

Comments

@joker-fu
Copy link

joker-fu commented Feb 22, 2019

dio: 2.0.10

class RequestInterceptor extends Interceptor{

@OverRide
onRequest(RequestOptions options) {

var headers = {"x-access-from": Constants.X_ACCESS_FROM};
if (Constants.X_ACCESS_ROLE != null) {
  headers["x-access-role"] = Constants.X_ACCESS_ROLE;
}
if (Constants.X_ACCESS_TOKEN != null) {
  headers["x-access-token"] = Constants.X_ACCESS_TOKEN;
}

options.headers = headers;

return options;

}
}
写了个拦截器添加统一header报错了,请求头添加成功了。

I/flutter (13951): DioError [DioErrorType.DEFAULT]: type 'int' is not a subtype of type 'String' of 'value'#0 __InternalLinkedHashMap&_HashVMBase&MapMixin&_LinkedHashMapMixin.[]= (dart:collection/runtime/libcompact_hash.dart)
I/flutter (13951): #1 Dio._transformData (package:dio/src/dio.dart:782:22)
I/flutter (13951):
I/flutter (13951): #2 Dio._makeRequest (package:dio/src/dio.dart:692:26)
I/flutter (13951):
I/flutter (13951): #3 Dio._request.. (package:dio/src/dio.dart:672:22)
I/flutter (13951): #4 _RootZone.runUnary (dart:async/zone.dart:1379:54)
I/flutter (13951): #5 _FutureListener.handleValue (dart:async/future_impl.dart:129:18)
I/flutter (13951): #6 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:642:45)
I/flutter (13951): #7 Future._propagateToListeners (dart:async/future_impl.dart:671:32)
I/flutter (13951): #8 Future._complete (dart:async/future_impl.dart:476:7)
I/flutter (13951): #9 _SyncCompleter.complete (dart:async/future_impl.dart:51:12)
I/flutter (13951): #10 _AsyncAwaitCompleter.complete (dart:async/runtime/libasync_patch.dart:28:18)
I/flutter (13951): #11 _completeOnAsyncReturn
I/flutter (13951): null

@wendux
Copy link
Contributor

wendux commented Feb 22, 2019

  1. 首先不建议在拦截器中直接覆盖options.headers,应该直接添加和修改,如:
options.headers["x-access-role"]=Constants.X_ACCESS_TOKEN;
  1. 你这个问题应该是Map类型不对, 你可以在定义Map时显式指定类型:
 var headers = Map<String, dynamic> {"x-access-from": Constants.X_ACCESS_FROM};

如果是这个原因的话,自己好好调一下,下个断点,就应该能找到原因。。。

@joker-fu
Copy link
Author

是的 覆盖是错误的 昨天就改了忘了回复了,但是能不能兼容呢

@wendux
Copy link
Contributor

wendux commented Feb 25, 2019

你这个主要是类型问题,如果保证前后key对应的value类型一致就没问题的。不建议覆盖是因为header中可能会有一些其他的字段,如dio全局header,覆盖了这些就没了。

@wendux wendux closed this as completed Feb 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants