text stringlengths 1 474 |
|---|
If your device doesn’t have a battery, |
it displays ‘Battery level not available’.<topic_end> |
<topic_start> |
Typesafe platform channels using Pigeon |
The previous example uses MethodChannel |
to communicate between the host and client, |
which isn’t typesafe. Calling and receiving |
messages depends on the host and client declaring |
the same arguments and datatypes in order for messages to work. |
You can use the Pigeon package as |
an alternative to MethodChannel |
to generate code that sends messages in a |
structured, typesafe manner.With Pigeon, the messaging protocol is defined |
in a subset of Dart that then generates messaging |
code for Android, iOS, macOS, or Windows. You can find a more complete |
example and more information on the pigeon |
page on pub.dev.Using Pigeon eliminates the need to match |
strings between host and client |
for the names and datatypes of messages. |
It supports: nested classes, grouping |
messages into APIs, generation of |
asynchronous wrapper code and sending messages |
in either direction. The generated code is readable |
and guarantees there are no conflicts between |
multiple clients of different versions. |
Supported languages are Objective-C, Java, Kotlin, C++, |
and Swift (with Objective-C interop).<topic_end> |
<topic_start> |
Pigeon example |
Pigeon file: |
<code_start>import 'package:pigeon/pigeon.dart'; |
class SearchRequest { |
final String query; |
SearchRequest({required this.query}); |
} |
class SearchReply { |
final String result; |
SearchReply({required this.result}); |
} |
@HostApi() |
abstract class Api { |
@async |
SearchReply search(SearchRequest request); |
}<code_end> |
Dart usage: |
<code_start>import 'generated_pigeon.dart'; |
Future<void> onClick() async { |
SearchRequest request = SearchRequest(query: 'test'); |
Api api = SomeApi(); |
SearchReply reply = await api.search(request); |
print('reply: ${reply.result}'); |
}<code_end> |
<topic_end> |
<topic_start> |
Separate platform-specific code from UI code |
If you expect to use your platform-specific code |
in multiple Flutter apps, you might consider |
separating the code into a platform plugin located |
in a directory outside your main application. |
See developing packages for details.<topic_end> |
<topic_start> |
Publish platform-specific code as a package |
To share your platform-specific code with other developers |
in the Flutter ecosystem, see publishing packages.<topic_end> |
<topic_start> |
Custom channels and codecs |
Besides the above mentioned MethodChannel, |
you can also use the more basic |
BasicMessageChannel, which supports basic, |
asynchronous message passing using a custom message codec. |
You can also use the specialized BinaryCodec, |
StringCodec, and JSONMessageCodec |
classes, or create your own codec.You might also check out an example of a custom codec |
in the cloud_firestore plugin, |
which is able to serialize and deserialize many more |
types than the default types.<topic_end> |
<topic_start> |
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, |
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.info Note |
On Android, the platform’s main thread is sometimes |
called the “main thread”, but it is technically defined |
as the UI thread. Annotate methods that need |
to be run on the UI thread with @UiThread. |
On iOS, this thread is officially |
referred to as the main thread.<topic_end> |
<topic_start> |
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.<topic_end> |
<topic_start> |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.