How to safely invoke native code in Flutter with Pigeon

Photo by Christian Wiediger on Unsplash

The standard platform channels use a standard message codec that supports efficient binary serialization of simple JSON-like values, such as booleans, numbers, Strings, byte buffers, and Lists and Maps of these (see StandardMessageCodec for details). The serialization and deserialization of these values to and from messages happens automatically when you send and receive values.

Photo by Alice on Unsplash

Setup the project

flutter pub add --dev

Defining the interface in Flutter

Generating the code

flutter pub run pigeon \
--input lib/services/pigeon.dart \
--dart_out lib/services/pigeon.g.dart \
--objc_header_out ios/Runner/pigeon.h \
--objc_source_out ios/Runner/pigeon.m \
--java_out ./android/app/src/main/java/it/angelocassano/news_app/Pigeon.java \
--java_package "it.angelocassano.news_app"

Final steps on Flutter

Switching to Android

EverythingResponse
EverythingResponse
PigeonArticleApi
PigeonArticleApi
MainActivity
MainActivity

Switching to iOS

Runner-Bridging-Header.h
Runner-Bridging-Header.h
EverythingResponseDTO
EverythingResponseDTO
PigeonArticleApi
PigeonArticleApi

Et voila!

Thank you for reading 👋

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store