text
stringlengths 1
372
|
|---|
<topic_start>
|
ReceivePorts and SendPorts
|
set up long-lived communication between isolates with two classes
|
(in addition to isolate):
|
ReceivePort and SendPort.
|
these ports are the only way isolates can communicate with each other.
|
ports behave similarly to streams,
|
in which the StreamController
|
or sink is created in one isolate,
|
and the listener is set up in the other isolate.
|
in this analogy,
|
the StreamConroller is called a SendPort,
|
and you can “add” messages with the send() method.
|
ReceivePorts are the listeners,
|
and when these listeners receive a new message,
|
they call a provided callback with the message as an argument.
|
for an in-depth explanation on setting up two-way
|
communication between the main isolate
|
and a worker isolate,
|
follow the examples in the dart documentation.
|
<topic_end>
|
<topic_start>
|
using platform plugins in isolates
|
as of flutter 3.7, you can use platform plugins in background isolates.
|
this opens many possibilities to offload heavy,
|
platform-dependent computations to an isolate that won’t block your UI.
|
for example, imagine you’re encrypting data
|
using a native host API
|
(such as an android API on android, an iOS API on iOS, and so on).
|
previously, marshaling data to the host platform could waste UI thread time,
|
and can now be done in a background isolate.
|
platform channel isolates use the BackgroundIsolateBinaryMessenger API.
|
the following snippet shows an example of using
|
the shared_preferences package in a background isolate.
|
<code_start>
|
import 'dart:isolate';
|
import 'package:flutter/services.dart';
|
import 'package:shared_preferences/shared_preferences.dart';
|
void main() {
|
// identify the root isolate to pass to the background isolate.
|
RootIsolateToken rootIsolateToken = RootIsolateToken.instance!;
|
Isolate.spawn(_isolateMain, rootIsolateToken);
|
}
|
future<void> _isolateMain(RootIsolateToken rootIsolateToken) async {
|
// register the background isolate with the root isolate.
|
BackgroundIsolateBinaryMessenger.ensureInitialized(rootIsolateToken);
|
// you can now use the shared_preferences plugin.
|
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
|
print(sharedPreferences.getBool('isDebug'));
|
}
|
<code_end>
|
<topic_end>
|
<topic_start>
|
limitations of isolates
|
if you’re coming to dart from a language with multithreading,
|
it’s reasonable to expect isolates to behave like threads,
|
but that isn’t the case.
|
isolates have their own global fields,
|
and can only communicate with message passing,
|
ensuring that mutable objects in an isolate are only ever accessible
|
in a single isolate.
|
therefore, isolates are limited by their access to their own memory.
|
for example,
|
if you have an application with a global mutable variable called configuration,
|
it is copied as a new global field in a spawned isolate.
|
if you mutate that variable in the spawned isolate,
|
it remains untouched in the main isolate.
|
this is true even if you pass the configuration object as a message
|
to the new isolate.
|
this is how isolates are meant to function,
|
and it’s important to keep in mind when you consider using isolates.
|
<topic_end>
|
<topic_start>
|
web platforms and compute
|
dart web platforms, including flutter web,
|
don’t support isolates.
|
if you’re targeting the web with your flutter app,
|
you can use the compute method to ensure your code compiles.
|
the compute() method runs the computation on
|
the main thread on the web,
|
but spawns a new thread on mobile devices.
|
on mobile and desktop platforms
|
await compute(fun, message)
|
is equivalent to await isolate.run(() => fun(message)).
|
for more information on concurrency on the web,
|
check out the concurrency documentation on dart.dev.
|
<topic_end>
|
<topic_start>
|
no rootBundle access or dart:ui methods
|
all UI tasks and flutter itself are coupled to the main isolate.
|
therefore,
|
you can’t access assets using rootBundle in spawned isolates,
|
nor can you perform any widget
|
or UI work in spawned isolates.
|
<topic_end>
|
<topic_start>
|
limited plugin messages from host platform to flutter
|
with background isolate platform channels,
|
you can use platform channels in isolates to send messages to the host platform
|
(for example android or iOS),
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.