πŸ”¬Native Integrations

Native integrations are crucial when you need to access platform-specific features or APIs that are not covered by existing Flutter plugins. Let's delve into how you can achieve this in Flutter.

1. Platform Channels:

Platform channels are the bridge between Flutter (Dart) code and the native code (Swift for iOS, Kotlin/Java for Android). They allow you to communicate between these different environments.

a. MethodChannel:

Dart Side:

dartCopy code// Dart
final channel = MethodChannel('samples.flutter.dev/battery');

Future<void> getBatteryLevel() async {
  final int batteryLevel = await channel.invokeMethod('getBatteryLevel');
}
  1. final channel = MethodChannel('samples.flutter.dev/battery'); - Here, you're creating a new MethodChannel with a unique name samples.flutter.dev/battery.

  2. getBatteryLevel is a function that will request the battery level from the native side.

  3. await channel.invokeMethod('getBatteryLevel'); - You're asking the native side to execute a method named getBatteryLevel and await its result.

Swift Side:

swiftCopy code// Swift
let channel = FlutterMethodChannel(name: "samples.flutter.dev/battery", binaryMessenger: controller.binaryMessenger)

channel.setMethodCallHandler {
  (call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in
  // Handle battery messages.
}
  1. let channel = FlutterMethodChannel(name: "samples.flutter.dev/battery", binaryMessenger: controller.binaryMessenger) - This line creates a method channel on the native side with the same name as on the Dart side.

  2. channel.setMethodCallHandler { ... } - This sets up a handler for method calls on this channel. When Dart asks for getBatteryLevel, this handler gets called.

b. EventChannel:

Event channels are used for communication that flows from the native code to Dart.

Dart Side:

dartCopy code// Dart
final channel = EventChannel('samples.flutter.dev/stream');
  • Here, you're creating a new EventChannel with a unique name samples.flutter.dev/stream.

Swift Side:

swiftCopy code// Swift
let channel = FlutterEventChannel(name: "samples.flutter.dev/stream", binaryMessenger: controller.binaryMessenger)
  • Similar to the MethodChannel, you create an EventChannel on the native side with the same name as on the Dart side.


2. Creating Platform-Specific Implementations:

Create a new project with platform-specific code using the following command:

flutter create --template=plugin -i swift -a kotlin my_plugin

This command specifies Swift for iOS and Kotlin for Android as the languages to be used for platform-specific code.


3. Handling Platform-Specific Code:

You can handle platform-specific code by creating separate directories for iOS and Android, and placing the respective native code within these directories.

lib/
  platform/
    android/
      android_plugin.dart
    ios/
      ios_plugin.dart

4. Calling Platform-Specific Code:

In your Dart code, you can conditionally call platform-specific code using Platform.isIOS or Platform.isAndroid.

if (Platform.isIOS) {
  // Call iOS code
} else if (Platform.isAndroid) {
  // Call Android code
}

Assignments πŸ“


Further Reading πŸ“š

By understanding and utilizing Flutter's platform channels and creating platform-specific implementations, you can extend your Flutter app with native functionalities and ensure a comprehensive and integrated user experience across both Android and iOS platforms.

Last updated