text
stringlengths
1
474
you might have better performance by creating isolates that don’t exit immediately.To do this, you can use a handful of lower-level isolate-related APIs that
Isolate.run abstracts:When you use the Isolate.run method,
the new isolate immediately shuts down after it
returns a single message to the main isolate.
Sometimes, you’ll need isolates that are long lived,
and can pass multiple messages to each other over time.
In Dart, you can accomplish this with the Isolate API
and Ports.
These long-lived isolates are colloquially known as background workers.Long-lived isolates are useful when you have a specific process that either
needs to be run repeatedly throughout the lifetime of your application,
or if you have a process that runs over a period of time
and needs to yield multiple return values to the main isolate.Or, you might use worker_manager to manage long-lived isolates.<topic_end>
<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,