Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Updated the platform channels documentation for background isolate channels #7592

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 32 additions & 7 deletions src/development/platform-integration/platform-channels.md
Original file line number Diff line number Diff line change
Expand Up @@ -969,13 +969,13 @@ types than the default types.

## Channels and platform threading

When invoking channels on the platform side destined
for Flutter, invoke them on the platform's main thread.
When invoking channels in Flutter destined
for the platform side, invoke on the root `Isolate`.
The platform side's handlers can execute
on the platform's main thread or they can execute on
a background thread if using a Task Queue.
When invoking channels on the platform side destined for Flutter,
invoke them on the platform's main thread.
When invoking channels in Flutter destined for the platform side,
either invoke them from any `Isolate` that is the root
`Isolate`, _or_ that is registered as a background `Isolate`.
The handlers for the platform side can execute on the platform's main thread
or they can execute on a background thread if using a Task Queue.
You can invoke the platform side handlers asynchronously
and on any thread when the Task Queue API is available;
otherwise, they must be invoked on the platform thread.
Expand All @@ -989,6 +989,31 @@ otherwise, they must be invoked on the platform thread.
referred to as [the main thread][].
{{site.alert.end}}

### Using plugins and channels from background isolates

Plugins and channels can be used by any `Isolate`, but that `Isolate` has to be
a root `Isolate` (the one created by Flutter) or registered as a background
`Isolate` for a root `Isolate`.

The following example shows how to register a background `Isolate` in order to
use a plugin from a background `Isolate`.

```dart
import 'package:flutter/services.dart';
import 'package:shared_preferences/shared_preferences.dart';

void _isolateMain(RootIsolateToken rootIsolateToken) async {
BackgroundIsolateBinaryMessenger.ensureInitialized(rootIsolateToken);
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
print(sharedPreferences.getBool('isDebug'));
}

void main() {
RootIsolateToken rootIsolateToken = RootIsolateToken.instance!;
Isolate.spawn(_isolateMain, rootIsolateToken);
}
```

### Executing channel handlers on background threads

In order for a channel's platform side handler to
Expand Down