跪拜 Guibai
← Back to the summary

With Flutter 3.50, Native Interop Is Stable and Platform Channel Is on Its Way Out

After the release of Flutter 3.50, Flutter Native Interop is now stable. Next up, Platform Channel is heading for retirement. It feels like it'll be about two versions before it's mostly gone. The current situation is roughly:

Previously, calling a native method from Dart, such as reading a system configuration on Android, even if it only needed to return an integer, required encoding the parameters with Codec, going asynchronously through BinaryMessenger, then the native side receives the message, decodes it, executes the method, and sends the result back with reverse encoding.

Although Pigeon can generate type-safe interfaces on both sides for plugins, Platform Channel is troublesome both in terms of performance ceiling and operations, and often freezes when the data gets large:

image

Now calls can go directly via JNI or FFI, at least eliminating message dispatch and whole-package encoding/decoding, and also providing synchronous calls. Adapting to native platform APIs is also much easier.

Now FFIgen can generate Dart bindings from C/Objective-C headers, and JNIgen generates bindings from Java/Kotlin classes, JARs, or AARs. In fact, if a project only uses a system service on Android, or directly wraps a C ABI library, you might not even need Pigeon. Just call the platform API directly from Dart.

On the iOS side, though, it's still a bit annoying. FFIgen still does not support parsing arbitrary Swift APIs directly. You first need to expose the Swift interface through an Objective-C compatible entry point, and then call it via the generated header. This is something to learn from Kotlin.

Pigeon works in the opposite direction: first use a Dart declaration to define the cross-platform API, then generate Dart and Swift/Kotlin interfaces. The actual functionality on the native platform is implemented by the developer. The original Channel communication layer is replaced by FFI/JNI.

image

The same Pigeon definition can generate two backends at the same time: Android, iOS, and macOS use FFI/JNI; Windows and Linux continue with Platform Channel, without splitting the business interface into two parts.

Actually, FFI already supported C interfaces, so that might not be necessary.

Another practical advantage of the new model is synchronous calls and background Isolates. Previously, using Platform Channel from a background Isolate required handling BackgroundIsolateBinaryMessenger. Now Pigeon Native Interop's Host API can be called directly from an Isolate, without needing to get RootIsolateToken first.

However, calls execute on the thread that initiates them, so @TaskQueue is not supported in this mode. That means a synchronous call to a time-consuming JNI method from Dart's main Isolate could still cause frame jank. When a background Isolate calls into interfaces that need to operate on Android Activity or iOS UIKit, the native implementation also needs to switch back to the platform main thread itself. But it's not a big problem, and overall performance is definitely improved.

So, it's time to retire the Channel in your projects.

Comments

Top 3 of 5 from juejin.cn, machine-translated. The original thread is authoritative.

伯伯熊勞

For iOS and Android mobile features, it's still more convenient to go through Platform Channel, right?

zhao1zhihui

Using Alibaba's development suite feels pretty good. Is this newly promoted thing something new from Flutter? I haven't paid attention for a long time.

恋猫de小郭

That's roughly it.

终极负能量plus

Previously with platform, the main thing was that multithreading and such could only be done at the native baseline level. If now with this new approach, how do you handle frequent thread operations? Can Dart withstand it?

pengzhaoyang100

Thumbs up