Kênh chanel

Contents Architectural overview: platkhung channelsExample: Calling platform-specific iOS & Android code using platsize channelsTypesafe platkhung channels via PigeonChannels and platkhung threading

This guide describes how to write custom platform-specific code. Someplatform-specific functionality is available through existing packages;see using packages.

Bạn đang xem: Kênh chanel

Note: The information in this page is valid for most plugins, except for any web plugin implemented as a federated plugin. Federated plugins don’t use platsize channels.

segala.info uses a flexible system that allows you to callplatform-specific APIs whether available in Kotlin orJava code on Android, or in Swift or Objective-C code on iOS.

segala.info’s builtin platform-specific API support does not rely on codegeneration, but rather on a flexible message passing style. Alternatively, thepackage Pigeon can be used for sending structured typesafe messages viacode generation:

The segala.info portion of the app sends messages lớn its host,the iOS or Android portion of the phầm mềm, over a platform channel.

The host listens on the platform channel, và receives the message.It then calls into lớn any number of platform-specific APIs—usingthe native programming language—& sends a response baông xã to theclient, the segala.info portion of the phầm mềm.

Note: This guide addresses using the platkhung channel mechanism if you need lớn use the platform’s APIs or libraries in Java, Kotlin, Objective-C, or Swift. But you can also write platform-specific Dart code in your segala.info tiện ích by inspecting the defaultTargetPlatform property. Platkhung adaptations lists some platform-specific adaptations that segala.info automatically does for you in the framework.

Architectural overview: platform channels

Messages are passed between the client (UI)& host (platform) using platformchannels as illustrated in this diagram:

*

Messages and responses are passed asynchronously,to lớn ensure the user interface remains responsive.

Note: Even though segala.info sends messages lớn & 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 sendingmessages that correspond to lớn method calls. On the platform side,MethodChannel on Android (MethodChannelAndroid) andsegala.infoMethodChannel on iOS (MethodChanneliOS)enable receiving method calls & sending back aresult. These classes allow you khổng lồ develop a platkhung pluginwith very little ‘boilerplate’ code.

Xem thêm: Bán Gì Ở Chợ Quê "1 Vốn 4 Lời" Hot Nhất 2021, Bán Gì Ở Chợ Quê Dễn Kiếm Tiền

Note: If desired, method calls can also be sent in the reverse direction, with the platsize acting as client lớn methods implemented in Dart. A concrete example of this is the quick_actions plugin.

Platkhung channel data types tư vấn và codecs

The standard platform channels use a standard message codec that supportsefficient binary serialization of simple JSON-like values, such as booleans,numbers, Strings, byte buffers, và Lists & Maps of these(see StandardMessageCodec) for details).The serialization and deserialization of these values lớn & frommessages happens automatically when you skết thúc and receive sầu values.

The following table shows how Dart values are received on theplatform side và vice versa:

Dart Java Kotlin Obj-C Swift
null null null nil (NSNull when nested) nil
bool java.lang.Boolean Boolean NSNumber numberWithBool: NSNumber(value: Bool)
int java.lang.Integer Int NSNumber numberWithInt: NSNumber(value: Int32)
int, if 32 bits not enough java.lang.Long Long NSNumber numberWithLong: NSNumber(value: Int)
double java.lang.Double Double NSNumber numberWithDouble: NSNumber(value: Double)
String java.lang.String String NSString String
Uint8List byte<> ByteArray segala.infoStandardTypedData typedDataWithBytes: segala.infoStandardTypedData(bytes: Data)
Int32List int<> IntArray segala.infoStandardTypedData typedDataWithInt32: segala.infoStandardTypedData(int32: Data)
Int64List long<> LongArray segala.infoStandardTypedData typedDataWithInt64: segala.infoStandardTypedData(int64: Data)
Float64List double<> DoubleArray segala.infoStandardTypedData typedDataWithFloat64: segala.infoStandardTypedData(float64: Data)
List java.util.ArrayList List NSArray Array
Map java.util.HashMap HashMap NSDictionary Dictionary

Example: Calling platform-specific iOS & Android code using platform channels

The following code demonstrates how to Call a platform-specific APIto lớn retrieve sầu & display the current battery màn chơi.It uses the Android BatteryManager API,and the iOS device.batteryLevel API, via a single platform message,getBatteryLevel().

The example adds the platform-specific code inside the main phầm mềm itself.If you want lớn reuse the platform-specific code for multiple apps,the project creation step is slightly different(see developing packages), but the platkhung channel codeis still written in the same way.

Note: The full, runnable source-code for this example is available in/examples/platform_channel/ for Android with Java andiOS with Objective-C. For iOS with Swift,see /examples/platform_channel_swift/.

Step 1: Create a new ứng dụng project

Start by creating a new app:

In a terminal run: segala.info create batterylevel

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:

In a terminal run: segala.info create -i objc -a java batterylevel

Step 2: Create the segala.info platsize client

The app’s State class holds the current ứng dụng state.Extover that khổng lồ hold the current battery state.

First, construct the channel. Use a MethodChannel with a singleplatsize method that returns the battery level.

Xem thêm: Dịch Vụ Đặt Hàng Order, Nhập Hàng Trung Quốc Về Việt Nam Giá Rẻ, Uy Tín

The client & host sides of a channel are connected througha channel name passed in the channel constructor.All channel names used in a single phầm mềm mustbe unique; prefix the channel name with a chất lượng ‘domainprefix’, for example: samples.segala.info/battery.


Chuyên mục: SEO