0

I am using compute to do some work while keeping the UI running. The compute was working until I added another http call before it.

The working code is as follow

final ListRequest request =
        ListRequest(baseUrl: env['SERVER_URL']!, path: '/Items');

_mainController.updateListItems(
        await compute(_service.getItems, request));

I read some articles saying the function compute calls should be a top level function or a static function. However, the getItems is an instance function and there was no exception.

Recently I added a few lines and the code became

final Filter? filter = await _service.getFilter();

final ListRequest request =
        ListRequest(baseUrl: env['SERVER_URL']!, path: '/Items');

request.filter = filter;

_mainController.updateListItems(
        await compute(_service.getItems, request));

getFilter is a http call to retrieve some filter parameters from the backend.

Then I got the following error

Invalid argument(s): Illegal argument in isolate message: (object extends NativeWrapper - Library:'dart:io' Class: _SecureFilterImpl@13069316)

My dart and flutter versions are

Dart SDK version: 2.15.1 (stable)
Flutter 2.8.1

Thank you

=========================================================

Update

The Filter is

Filter {
  String? itemLocationSuburb;
  String? itemLocationPostcode;
}
Ben
  • 957
  • 1
  • 11
  • 37
  • Can you provide the definition for the `Filter` class? My guess is that `Filter` has a property of a type that is not supported by [SendPort.send](https://api.dart.dev/stable/2.17.3/dart-isolate/SendPort/send.html). – mmcdon20 Jun 18 '22 at 19:13
  • Hi @mmcdon20, thank you for your advice. I added the implementation of Filter. Both of them are nullable string so not sure what could be wrong. – Ben Jun 19 '22 at 00:54
  • Nullable Strings should work fine, so I guess that isn't it. Is it possible to create a [minimal complete reproducible example](https://stackoverflow.com/help/minimal-reproducible-example)? – mmcdon20 Jun 19 '22 at 01:10
  • Thank you @mmcdon20, I will come back when the example is created. – Ben Jun 20 '22 at 13:57

1 Answers1

0

Your _service service presumably contains a HttpClient. When you make a request through this client, it opens a connection to the HTTP server, and may maintain the connection after the request completes.

The HttpClient cannot be sent through a SendPort when it has open connections, but it is included in the scope of the getItems method.

To work around this issue, you can do one of the following:

  • Disable persistent connections with the HttpClientRequest.persistentConnection property
  • Make a new HttpClient to send through the compute function every time
  • Implement a long-lived background isolate to maintain its own HttpClient
  • Use the HttpClient in the main isolate, and only perform other work like parsing with compute (there's no significant benefit to using an isolate to make HTTP requests anyway)
hacker1024
  • 3,186
  • 1
  • 11
  • 31