Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Getting the common "null check operator on a null value" when testing using Chopper client

I’m testing my repository code that use Chopper, and am now getting the below errors, that isn’t in my code:

Null check operator used on a null value
package:chopper/src/interceptor.dart 195:58
HttpLoggingInterceptor.onResponse package:chopper/src/base.dart 215:23
ChopperClient._interceptResponse package:chopper/src/base.dart 346:17
ChopperClient._processResponse package:chopper/src/base.dart 341:12
ChopperClient.send

I’ve created a static method that return my own chopper service as follows:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  static MyChopperService create(String baseUrl, [http.Client? client]) {
    return _$MyChopperService (
      ChopperClient(
        baseUrl: baseUrl,
        client: client,
        services: [_$MyChopperService ()],
        interceptors: [
              HttpLoggingInterceptor(),
              CurlInterceptor(),
        ],
        converter: const JsonConverter(),
      ),
    );
  }

In my unit tests I create the service as follows:

  final MockClient mockClient = MockClient((req) async => Response('return data', 200));
  final MyChopperService service = MyChopperService.create('baseUrl', client: mockClient);
  ... then I go ahead and use this calling my repository.

Any ideas what I’m doing wrong?

>Solution :

Based on what I can see, you have two options:

  1. When creating the Response in the MockClient, also add a Request object that carries the Uri/Url. That is missing which seems to be something that the Chopper library haven’t guarded against.

Edit: Meaning you could do something like this:

Response('return data', 200, request: Request('GET', Uri(scheme: 'https', host: 'test.dev')))
  1. Let your service-creator take in interceptors as an argument and pass none into it when running your tests. That way the code causing the error won’t be executed.

So change the method signature to instead have this:

{http.Client? client, List<dynamic>? interceptors}

and use this in the ChopperClient constructor:

interceptors: interceptors ??
[
    HttpLoggingInterceptor(),
    CurlInterceptor(),
],

Then call it in your test like this:

MyChopperService.create('baseUrl', client: mockClient, interceptors: []);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading