Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Interacting with native code & plugins in Flutter

Interacting with native code & plugins in Flutter

In this presentation, I will show you how to connect your Flutter app with platform native code for iOS & Android. We will go through how Flutter interacts with native platforms under the hood, which APIs to use, and how to create a new plugin step by step.

Alek Åström

January 29, 2020
Tweet

More Decks by Alek Åström

Other Decks in Programming

Transcript

  1. Agenda • Flutter under the hood • Why interact with

    native? • Different ways of interacting with native code • Platform Channel • Dart:ffi (bonus) • Creating a plugin (w/ live coding!) • Considerations & Learnings
  2. Flutter Internals • Flutter is built with Dart on top

    of a C++ engine • Does its own drawing with Skia – no platform UI
  3. No native rendering needed High performance scrolling 60fps animations No

    bridge hopping when listening to scroll events No drawing / layout bugs between platforms
  4. Why interact with platform native code? Platform-specific functionality & APIs

    e.g. Notifications Access hardware e.g. Camera, GPS, Speaker Irreproducible native views (like Maps) Existing libraries, e.g. SQLite, Intercom, Firebase ⛔ Not for drawing / layout, unlike React Native
  5. How do Flutter apps run? • Flutter apps are hosted

    as frameworks inside native apps • The hosts are regular native apps. • FlutterView (Android) + FlutterViewController (iOS) is the bridge, only one per app. https://flutter.dev/docs/development/platform-integration/platform-channels
  6. FlutterViewController & FlutterView • Forwards touches, keyboard, lifecycle events •

    Handles communication with native plugins • Passes messages between Dart and platform native code (Objective-C/Swift/Java/Kotlin) • …using Platform Channels https://flutter.dev/docs/development/platform-integration/platform-channels
  7. Plugin The thing you get when combining Flutter, iOS &

    Android code together into a dart package
  8. Creating a plugin • Creates a package with iOS, Android

    and Dart code • + an example Flutter app for both platforms • Publish with
  9. Creating a plugin • Creates a package with iOS, Android

    and Dart code • + an example Flutter app for both platforms • Publish with Android iO S Flutter
  10. Platform Channels https://flutter.dev/docs/development/platform-integration/platform-channels • Identified by name e.g. ”flutter/lifecycle” •

    Passes binary messages • Uses a codec for serialization / deserialization • Can both send and receive • Used only on the main thread A simple mechanism for communicating between Dart code and platform code inside a hosted Flutter app.
  11. Method Channels ✅ • Used to invoke different methods on

    the same channel to group functionality • Sends a ”method” name and parameters • Uses StandardMessageCodec to parse params & return values https://flutter.dev/docs/development/platform-integration/platform-channels
  12. Event Channels ✅ • Used to send events over a

    stream • Example: Listening to device sensors (GPS, Accelerometer, Gyro) • Uses StandardMessageCodec just like method channels
  13. Plugin dependencies Your plugins can have dependencies! 1. Other Dart

    packages (pubspec.yaml) 2. iOS CocoaPods (Podfile) 3. Gradle modules (build.gradle) Common Flutter plugins include SDK wrappers for native 3rd party frameworks • Intercom, Firebase, AWS
  14. Learnings & Considerations • Use the host app where you

    run into plugin limitations • Platform channels have built in mocking. Use them for testing • channel.setMockMethodCallHandler • Be wary of Dart types
  15. Bonus – Dart:ffi • A mechanism for directly calling functions

    exposed to the C/C++ runtime • Currently in beta (flutter channel dev; flutter upgrade) • Used to interact directly with native frameworks & functions, such as sqlite