http icon indicating copy to clipboard operation
http copied to clipboard

NoSuchMethodError: method not found: '$index' on null TypeError: Cannot read properties of undefined (reading '$index')

Open creativecreatorormaybenot opened this issue 3 years ago • 0 comments

When trying to make an HTTP request in a Flutter app, the following error is thrown (in release/profile mode only):

NoSuchMethodError: method not found: '$index' on null TypeError: Cannot read properties of undefined (reading '$index')
    at Object._contentTypeForHeaders (http://localhost:4200/main.dart.js:54170:33)
    at http://localhost:4200/main.dart.js:127425:84
    at _wrapJsFunctionForAsync_closure.$protected (http://localhost:4200/main.dart.js:10994:15)
    at _wrapJsFunctionForAsync_closure.call$2 (http://localhost:4200/main.dart.js:112675:12)
    at _awaitOnObject_closure.call$1 (http://localhost:4200/main.dart.js:112663:32)
    at _RootZone.runUnary$2$2 (http://localhost:4200/main.dart.js:115506:18)
    at _Future__propagateToListeners_handleValueCallback.call$0 (http://localhost:4200/main.dart.js:113640:51)
    at Object._Future__propagateToListeners (http://localhost:4200/main.dart.js:11279:93)
    at _Future._completeWithValue$1 (http://localhost:4200/main.dart.js:113477:9)
    at _AsyncAwaitCompleter.complete$1 (http://localhost:4200/main.dart.js:112647:14)

This only points to the transpiled file, but the relevant code (the first two entries in the stack trace) are:

Click to expand
    _contentTypeForHeaders(headers) {
      var contentType = headers.$index(0, "content-type");
      if (contentType != null)
        return A.MediaType_MediaType$parse(contentType);
      return A.MediaType$("application", "octet-stream", null);
    },

And:

            case 3:
              // returning from await.
              response = $async$result;
              body = B.C_JsonCodec.decode$1(0, A.encodingForCharset(J.$index$asx(A._contentTypeForHeaders(response.headers).parameters._collection$_map, "charset")).decode$1(0, response.bodyBytes));
              t1 = response.statusCode;
              if (!(t1 >= 200 && t1 < 300))
                throw A.wrapException(new A.AlgoliaError(body, t1));
              $async$returnValue = A.AlgoliaQuerySnapshot$_(t2, t3, body);
              // goto return
              $async$goto = 1;
              break;

This translate to the following lines. One is from the algolia Dart package:

https://github.com/knoxpo/dart_algolia/blob/90df241b10b8bf635cefbf437bacc2b03fdd4f51/lib/src/query.dart#L82

Which calls Response.body:

https://github.com/dart-lang/http/blob/1e42ffa181b263f7790f276a5465832bff7ce615/lib/src/response.dart#L28

And this calls _encodingForHeaders which calls _contentTypeForHeaders which for some reason calls ['content-type'] on null (even though headers is non-nullable):

https://github.com/dart-lang/http/blob/1e42ffa181b263f7790f276a5465832bff7ce615/lib/src/response.dart#L84