Dio: run example/adapter.dart: NoSuchMethodError: The method '[]' was called on null

Created on 10 Oct 2019  Â·  7Comments  Â·  Source: flutterchina/dio

When I run dart example/adapter.dart with master branch and dart 2.5.1, there is an error whose content is

➜  dio git:(master) ✗ dart example/adapter.dart 
Unhandled exception:
DioError [DioErrorType.DEFAULT]: NoSuchMethodError: The method '[]' was called on null.
Receiver: null
Tried calling: []("content-type")
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
#1      DefaultTransformer.transformResponse (package:dio/src/transformer.dart:129:37)
<asynchronous suspension>
#2      DioMixin._dispatchRequest (package:dio/src/dio.dart:923:38)
<asynchronous suspension>
#3      DioMixin._request._interceptorWrapper.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:dio/src/dio.dart:828:37)
#4      DioMixin.checkIfNeedEnqueue (package:dio/src/dio.dart:1099:22)
#5      DioMixin._request._interceptorWrapper.<anonymous closure>.<anonymous closure> (package:dio/src/dio.dart:825:22)
#6      new Future.<anonymous closure> (dart:async/future.dart:176:37)
#7      Timer._createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:21:15)
#8      _Timer._runTimers (dart:isolate-patch/timer_impl.dart:382:19)
#9      _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:416:5)
#10     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:172:12)

#0      DioMixin._dispatchRequest (package:dio/src/dio.dart:942:7)
<asynchronous suspension>
#1      DioMixin._request._interceptorWrapper.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:dio/src/dio.dart:828:37)
#2      DioMixin.checkIfNeedEnqueue (package:dio/src/dio.dart:1099:22)
#3      DioMixin._request._interceptorWrapper.<anonymous closure>.<anonymous closure> (package:dio/src/dio.dart:825:22)
#4      new Future.<anonymous closure> (dart:async/future.dart:176:37)
#5      Timer._createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:21:15)
#6      _Timer._runTimers (dart:isolate-patch/timer_impl.dart:382:19)
#7      _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:416:5)
#8      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:172:12)

And I also find the same issue when I use HttpClientAdapter whose version is v3.0.3 in my own project. Could you check this issue? Thanks.

All 7 comments

After upgrading from 2.1.7 to 3.0.3, I am experiencing a similar error when using HttpClientAdapter.

Using adapter must set headers at ResponseBody.fromString,otherwise it will cause the above exception .

Using adapter must set headers at ResponseBody.fromString,otherwise it will cause the above exception .

I find it will be solved by adding response.headers != null to response check condition.
Good job !

Fixed at v3.0.4

This issue happens to me in latest version (dio: '3.0.9')
DioError (DioError [DioErrorType.DEFAULT]: NoSuchMethodError: The method '[]' was called on null

Any help?

I solved this problem by extending receiveTimeout of Dio BaseOptions.

I get this error when I try to use custom RequestOptions to test my POST request using Dio:

final RequestOptions testRequestOptions = RequestOptions(method: "POST");
final ResponseBody testSuccessResponseBody = ResponseBody.fromString("{}", 201, headers: {});

when(mockDioAdapter.fetch(testRequestOptions, any, any)).thenAnswer((_) async => testSuccessResponseBody);

Error:

DioError [DioErrorType.DEFAULT]: NoSuchMethodError: The getter 'headers' was called on null.
Receiver: null
Tried calling: headers
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
#1      DioMixin._dispatchRequest (package:dio/src/dio.dart:927:43)
<asynchronous suspension>
#2      DioMixin._request._interceptorWrapper.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:dio/src/dio.dart:849:37)
#3      DioMixin.checkIfNeedEnqueue (package:dio/src/dio.dart:1121:22)
#4      DioMixin._request._interceptorWrapper.<anonymous closure>.<anonymous closure> (package:dio/src/dio.dart:846:22)
#5      new Future.<anonymous closure> (dart:async/future.dart:175:37)
#6      StackZoneSpecification._run (package:stack_trace/src/stack_zone_specification.dart:208:15)
#7      StackZoneSpecification._registerCallback.<anonymous closure> (package:stack_trace/src/stack_zone_specification.dart:116:48)
#8      _rootRun (dart:async/zone.dart:1182:47)
#9      _CustomZone.run (dart:async/zone.dart:1093:19)
#10     _CustomZone.runGuarded (dart:async/zone.dart:997:7)
#11     _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1037:23)
#12     StackZoneSpecification._run (package:stack_trace/src/stack_zone_specification.dart:208:15)
#13     StackZoneSpecification._registerCallback.<anonymous closure> (package:stack_trace/src/stack_zone_specification.dart:116:48)
#14     _rootRun (dart:async/zone.dart:1190:13)
#15     _CustomZone.run (dart:async/zone.dart:1093:19)
#16     _CustomZone.bindCallback.<anonymous closure> (dart:async/zone.dart:1021:23)
#17     Timer._createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:18:15)
#18     _Timer._runTimers (dart:isolate-patch/timer_impl.dart:397:19)
#19     _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:428:5)
#20     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)
Was this page helpful?
0 / 5 - 0 ratings