Flutter's seamless integration with native platforms is one of its standout features, enabling developers to harness platform-specific functionalities while enjoying the benefits of cross-platform development. This integration is made possible through Platform Channels and Dart's Foreign Function Interface (FFI).
Platform Channel
Platform Channels are a crucial feature in Flutter, serving as a bridge between Dart and native platform code. They enable Flutter apps to communicate with the underlying platform, whether it's Android, iOS, macOS, Linux, or Windows. This communication is essential for accessing native functionalities like cameras, GPS, sensors, and other platform-specific features that aren't directly accessible through Flutter.
Platform Channel offer three main types of communication methods:
Method Channel: This is used for asynchronous method invocations. It's handy when you need performs a specific function and return a result, such as fetching sensor data, checking battery status, or processing data using native capabilities.
Event Channel: This is used for create a data stream from native code to Dart. It's ideal for listening to continuous or periodic native events, like sensor readings or location updates.
BasicMessageChannel: This is used for asynchronous message exchanges, suited for simple communication. It supports various data serialization formats and codecs, such as JSON, binary, or custom-defined formats.
Now, let’s build a simple application where the Flutter part (Dart code) communicates with the native platform (Android & iOS) to retrieve the device’s battery level. We'll be using MethodChannel in flutter.
Android platform
Define the MethodChannel in Flutter
Handling the Method Call in Native Android (Kotlin)
In our Android project, we need to override the configureFlutterEngine method in our MainActivity or the relevant activity.
Call the method from Flutter
We can call getBatteryLevel from our Flutter code to retrieve the battery level from the native platform.
Run the App
To run our app on an Android device, the getBatteryLevel method will invoke the native platform code, and the battery level will be fetched. Always ensure that the channel name matches between the dart code and the native code.
iOS Platform
Run this command to open the Xcode project
open ios/Runner.xcworkspace
Next, open the appDelegate.swift file located under Runner > Runner in the Project Navigator.
Handling the Method Call in Native iOS (Swift)
Override the application function and create a FlutterMethodChannel tied to the same channel name as before. Then, add the Swift code that uses iOS battery APIs to retrieve the battery level.
Run our App
Now, run our app on iOS device. if you're using the iOS simulator, keep in mind that it doesn’t support the battery API
With Flutter, we can seamlessly integrate with native platform. Platform Channels bridge the gap between Dart and native code, allowing Flutter apps to communicate with the underlying platform. Additionally, We can use Pigeon, Dart FFI (Foreign Function Interface), FFIgen, and JNIgen (Java Native Interface Generator) to further simplify the process of creating bindings and facilitating communication between Dart and native code.