text stringlengths 1 372 |
|---|
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}'."; |
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.