admin管理员组

文章数量:1397102

I'm trying to use Dart isolates to offload JSON parsing in my Flutter package. However, when I run the following function, I get an error about an "unsendable object" in isolate messages.

Future<Map<String, dynamic>?> _extractResponse(String url) async {
  try {
    final response = await fetchWithRetry(url, maxAttempts: maxAttempts ?? 3);
    if (response.statusCode == 200) {
      print('fetching result');
      final jsonMap = await Isolate.run(() {
        return _requestAndContent(response.body);
      });...

Error

Invalid argument(s): Illegal argument in isolate message: object is unsendable 
- Library:'dart:async' Class: _Future@4048458
(see restrictions listed at `SendPort.send()` documentation for more information)
 <- _streamFuture in Instance of '_HttpClientConnection' (from dart:_http)
 <- hashCode in Instance of '_HashSetEntry<_HttpClientConnection>' (from dart:collection)
 <- _List len:8 (from dart:core)
 <- _buckets in Instance of '_HashSet<_HttpClientConnection>' (from dart:collection)
 <- _idle in Instance of '_ConnectionTarget' (from dart:_http)
 <- value in Instance of '_HashMapEntry' (from dart:collection)
 <- _List len:8 (from dart:core)...
  • Changing _requestAndContent to be synchronous instead of async
  • response.body.toString()

I'm trying to use Dart isolates to offload JSON parsing in my Flutter package. However, when I run the following function, I get an error about an "unsendable object" in isolate messages.

Future<Map<String, dynamic>?> _extractResponse(String url) async {
  try {
    final response = await fetchWithRetry(url, maxAttempts: maxAttempts ?? 3);
    if (response.statusCode == 200) {
      print('fetching result');
      final jsonMap = await Isolate.run(() {
        return _requestAndContent(response.body);
      });...

Error

Invalid argument(s): Illegal argument in isolate message: object is unsendable 
- Library:'dart:async' Class: _Future@4048458
(see restrictions listed at `SendPort.send()` documentation for more information)
 <- _streamFuture in Instance of '_HttpClientConnection' (from dart:_http)
 <- hashCode in Instance of '_HashSetEntry<_HttpClientConnection>' (from dart:collection)
 <- _List len:8 (from dart:core)
 <- _buckets in Instance of '_HashSet<_HttpClientConnection>' (from dart:collection)
 <- _idle in Instance of '_ConnectionTarget' (from dart:_http)
 <- value in Instance of '_HashMapEntry' (from dart:collection)
 <- _List len:8 (from dart:core)...
  • Changing _requestAndContent to be synchronous instead of async
  • response.body.toString()
Share Improve this question asked Mar 26 at 19:17 Jonathan GetachewJonathan Getachew 11 bronze badge 1
  • 1 Perhaps _requestAndContent returns a future? If so, put an await in front of it, and make the closure async. – Randal Schwartz Commented Mar 27 at 3:29
Add a comment  | 

1 Answer 1

Reset to default 0

Your closure contains a reference to the entire response since it does response.body inside the closure.

The response is a complicated object, which includes some futures and stream. You likely want to only send the body itself (which I'm guessing is a String - if not, you have bigger issues).

Consider:

final jsonMap = await Isolate.run(_createRequest(response.body));

//...

Object? _createRequest(String body) => () => _requestAndContent(response.body);

Even better, perform the request itself in the other isolate, so you don't have to serialize the body.

本文标签: Dart Isolate Error Illegal argument in isolate message object is unsendableStack Overflow