Slide 1

Slide 1 text

Interacting with native code in Flutter For iOS & Android

Slide 2

Slide 2 text

Hello, I’m Alek @MisterAlek github.com/MrAlek CTO @ doneservices.co

Slide 3

Slide 3 text

Discount Code FLUTTER300

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

Flutter Internals • Flutter is built with Dart on top of a C++ engine • Does its own drawing with Skia – no platform UI

Slide 6

Slide 6 text

No native rendering needed High performance scrolling 60fps animations No bridge hopping when listening to scroll events No drawing / layout bugs between platforms

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

Examples

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

Plugin The thing you get when combining Flutter, iOS & Android code together into a dart package

Slide 12

Slide 12 text

pub.dev

Slide 13

Slide 13 text

pubspec.yaml

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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.

Slide 17

Slide 17 text

BasicMessageChannel

Slide 18

Slide 18 text

BasicMessageChannel Swift Kotlin

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

MethodChannel

Slide 21

Slide 21 text

MethodChannel Swift Kotlin

Slide 22

Slide 22 text

Event Channels ✅ • Used to send events over a stream • Example: Listening to device sensors (GPS, Accelerometer, Gyro) • Uses StandardMessageCodec just like method channels

Slide 23

Slide 23 text

EventChannel

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

Read more • https://flutter.dev/docs/development/platform-integration/platform-channels • https://github.com/flutter/flutter/ • https://github.com/flutter/plugins/tree/master/packages/battery • https://medium.com/flutter/flutter-platform-channels-ce7f540a104e ”Flutter Platform Channels” by Mikkel Ravn

Slide 28

Slide 28 text

Thank you! done. Psst, we’re still hiring @MisterAlek github.com/mralek