Sub topic 12
Calling Plugins from a Background Isolate
Assume you know Dart async/await, Future, basic streams, and Flutter platform channels. This chapter focuses on native Flutter platforms, where a background isolate can sometimes call a plugin.
Why plugin calls need setup
An isolate does not automatically have access to Flutter's platform channel system. Before a background isolate calls a compatible plugin, it must initialize a BackgroundIsolateBinaryMessenger.
The root isolate obtains a RootIsolateToken. The token identifies the Flutter engine that owns the platform channels. Pass that token into the background isolate, then initialize the messenger there.
import 'dart:isolate';
import 'package:flutter/services.dart';
Future<String?> readValueInBackground() async {
final token = RootIsolateToken.instance;
if (token == null) {
throw StateError('Flutter is not running in a root isolate');
}
return Isolate.run(() async {
BackgroundIsolateBinaryMessenger.ensureInitialized(token);
const channel = MethodChannel('example/background');
return channel.invokeMethod<String>('readValue');
});
}The native side must implement the example/background channel and its readValue method. The Dart code sends the method call through the engine after the background messenger has been initialized.
The result comes back as a Future, just like a plugin call made on the root isolate.
Which plugins work
Background-isolate support is a plugin feature, not a guarantee provided by Flutter. A plugin must use platform channels in a way that supports calls from a background isolate. Check the plugin's documentation and test the target platforms.
| Plugin behavior | Background-isolate result |
|---|---|
| Explicitly supports background isolates | Usually usable after messenger initialization |
| Uses only Dart code | Usable if its data and dependencies are isolate-safe |
| Requires a Flutter view or UI context | Usually unavailable |
| Uses a plugin that assumes root-isolate initialization | May throw, hang, or return an invalid result |
A plugin may also need its own initialization before use. For example, a database plugin could require opening a separate connection in the worker. Do not assume that an object created on the root isolate can be reused in another isolate.
Message flow and ownership
The root isolate owns the engine's normal plugin registration. The background isolate sends a channel request through the initialized messenger. Native code handles the request and returns a value or an error.
Only send values that Dart can transfer between isolates. Return simple values such as strings, numbers, booleans, lists, and maps. Do not try to send a MethodChannel, plugin instance, widget, BuildContext, or open resource from one isolate to another. Create isolate-local objects inside the worker when needed.
Errors and platform differences
A plugin error completes the returned Future with an exception. Handle it at the caller:
Future<void> loadValue() async {
try {
final value = await readValueInBackground();
print('Value: $value');
} on PlatformException catch (error) {
print('Plugin failed: ${error.code}');
} on MissingPluginException {
print('This platform does not implement the method');
}
}Test Android and iOS separately. A plugin can support background calls on one platform but not the other. On the web, Flutter plugins and isolate behavior have different constraints, and this native background-messenger pattern does not generally apply.
Use a background isolate for a plugin call only when the plugin documents support and the work is substantial enough to justify the extra setup. Keep UI operations, widget access, and unsupported plugin calls on the root isolate.
Exercise
Why must a background isolate receive RootIsolateToken before calling a compatible plugin?
Show answer
The token lets BackgroundIsolateBinaryMessenger connect the isolate's platform-channel messages to the Flutter engine that owns the plugin.