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()
- 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.
本文标签: Dart Isolate Error Illegal argument in isolate message object is unsendableStack Overflow
版权声明:本文标题:Dart Isolate Error: Illegal argument in isolate message: object is unsendable - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744130232a2592143.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论