text
stringlengths
1
474
It then calls into any number of platform-specific APIs—using
the native programming language—and sends a response back to the
client, the Flutter portion of the app.info Note
This guide addresses using the platform channel mechanism
if you need to use the platform’s APIs in a non-Dart language.
But you can also write platform-specific Dart code
in your Flutter app by inspecting the
defaultTargetPlatform property.
Platform adaptations lists some
platform-specific adaptations that Flutter
automatically performs for you in the framework.<topic_end>
<topic_start>
Architectural overview: platform channels
Messages are passed between the client (UI)
and host (platform) using platform
channels as illustrated in this diagram:Messages and responses are passed asynchronously,
to ensure the user interface remains responsive.info Note
Even though Flutter sends messages to and from Dart asynchronously,
whenever you invoke a channel method, you must invoke that method on the
platform’s main thread. See the section on threading
for more information.On the client side, MethodChannel enables sending
messages that correspond to method calls. On the platform side,
MethodChannel on Android (MethodChannelAndroid) and
FlutterMethodChannel on iOS (MethodChanneliOS)
enable receiving method calls and sending back a
result. These classes allow you to develop a platform plugin
with very little ‘boilerplate’ code.info Note
If desired, method calls can also be sent in the reverse direction,
with the platform acting as client to methods implemented in Dart.
For a concrete example, check out the quick_actions plugin.<topic_end>
<topic_start>
Platform channel data types support and codecs
The standard platform channels use a standard message codec that supports
efficient binary serialization of simple JSON-like values, such as booleans,
numbers, Strings, byte buffers, and Lists and Maps of these
(see StandardMessageCodec for details).
The serialization and deserialization of these values to and from
messages happens automatically when you send and receive values.The following table shows how Dart values are received on the
platform side and vice versa:<topic_end>
<topic_start>
Example: Calling platform-specific code using platform channels
The following code demonstrates how to call
a platform-specific API to retrieve and display
the current battery level. It uses
the Android BatteryManager API,
the iOS device.batteryLevel API,
the Windows GetSystemPowerStatus API,
and the Linux UPower API with a single
platform message, getBatteryLevel().The example adds the platform-specific code inside
the main app itself. If you want to reuse the
platform-specific code for multiple apps,
the project creation step is slightly different
(see developing packages),
but the platform channel code
is still written in the same way.info Note
The full, runnable source-code for this example is
available in /examples/platform_channel/
for Android with Java, iOS with Objective-C,
Windows with C++, and Linux with C.
For iOS with Swift,
see /examples/platform_channel_swift/.<topic_end>
<topic_start>
Step 1: Create a new app project
Start by creating a new app:By default, our template supports writing Android code using Kotlin,
or iOS code using Swift. To use Java or Objective-C,
use the -i and/or -a flags:<topic_end>
<topic_start>
Step 2: Create the Flutter platform client
The app’s State class holds the current app state.
Extend that to hold the current battery state.First, construct the channel. Use a MethodChannel with a single
platform method that returns the battery level.The client and host sides of a channel are connected through
a channel name passed in the channel constructor.
All channel names used in a single app must
be unique; prefix the channel name with a unique ‘domain
prefix’, for example: samples.flutter.dev/battery.
<code_start>import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';<code_end>
<code_start>class _MyHomePageState extends State<MyHomePage> {
static const platform = MethodChannel('samples.flutter.dev/battery');
// Get battery level.<code_end>
Next, invoke a method on the method channel,
specifying the concrete method to call using
the String identifier getBatteryLevel.
The call might fail—for example,
if the platform doesn’t support the
platform API (such as when running in a simulator),
so wrap the invokeMethod call in a try-catch statement.Use the returned result to update the user interface state in _batteryLevel
inside setState.
<code_start>// Get battery level.
String _batteryLevel = 'Unknown battery level.';
Future<void> _getBatteryLevel() async {
String batteryLevel;
try {
final result = await platform.invokeMethod<int>('getBatteryLevel');
batteryLevel = 'Battery level at $result % .';
} on PlatformException catch (e) {
batteryLevel = "Failed to get battery level: '${e.message}'.";
}
setState(() {