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()
- 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
1 Answer
Reset to default 0Your 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.