Flutter University
  • πŸ‘‹Welcome to Flutter University
  • Learn Flutter
    • πŸš—Basics
      • 🧺Dart Basics
      • πŸš›Setup & Installation
      • 🐣Hello Flutter
      • πŸŒ‰Widgets
      • ⛸️Basic State Management
      • πŸ‡ΎπŸ‡ΉBasic Layout and Styling
      • 🐝Basic Interactivity
      • πŸ›£οΈNavigation
      • πŸͺ„Working with Assets
    • πŸš…Intermediate
      • 🎯Deeper into Dart
      • ⭐More on State Management
      • πŸ“ƒForm Handling
      • πŸ—ΌNetworking
      • πŸŽ‡Persistence
      • πŸ§™β€β™‚οΈAnimations
      • πŸ§ͺTesting
      • πŸ“¦Package Management
    • ✈️Professional
      • πŸŽ“Advanced Animations
      • 🎨Custom Painters
      • 🐼Continuous Integration/Continuous Deployment (CI/CD)
      • 🎭Performance Profiling
      • πŸ”¬Native Integrations
      • 🌍Accessibility and Localization
      • 🀘Understanding Design Patterns
      • πŸ“šFlutter Architecture
        • The Layer Model
        • Reactive User Interfaces
        • Flutter Widgets
        • The Rendering Process
        • Platform Embedders Overview
        • Integrating with Other Code
        • Support for the Web
  • Tutorials
    • 🌈UI
      • 🏚️Clubhouse Clone
      • πŸ”‰Netflix Clone
    • βš”οΈFull Stack
    • ⛓️Blockchain
    • πŸ€–AI/ML
  • Miscellaneous
    • πŸ–₯️100 Days of Flutter
    • 🎨Join Community
Powered by GitBook
On this page
  • 1. Platform Channels:
  • a. MethodChannel:
  • b. EventChannel:
  • 2. Creating Platform-Specific Implementations:
  • 3. Handling Platform-Specific Code:
  • 4. Calling Platform-Specific Code:
  • Assignments πŸ“
  • Further Reading πŸ“š

Was this helpful?

  1. Learn Flutter
  2. Professional

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:

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:

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:

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

Swift Side:

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 12 months ago

Was this helpful?

✈️
πŸ”¬
Flutter Platform Channels
Writing custom platform-specific code