FlutterAdvanced 3 min read

Flutter native interop: FFIgen, JNIgen and Pigeon

Choose a native boundary for Dart, Kotlin, Java or Objective-C and keep platform details out of widgets.

On this page

A cross-platform UI still needs a deliberate boundary when using native capabilities. Start by naming the operation your app needs, then choose an integration approach based on the native API and the platforms you ship.

Choose the direction of the contract

FFIgen and JNIgen start from native headers or classes and generate low-level Dart bindings. Pigeon starts with a Dart contract and generates matching platform interfaces. Direct calls can avoid channel serialization, but they also make native object lifetimes and threading important design concerns.

Flutter 3.50 stabilizes Objective-C FFIgen and Java/Kotlin JNIgen support. Pigeon’s direct native-interoperability mode remains a preview; its existing channel-based approach is a separate option. Use the interop guide to select the generator for the actual target language.

Write the application interface first

Suppose your app needs an availability check for a device feature. Define a small Dart interface that describes the app’s need:

abstract interface class DeviceCapability {
  Future<bool> isAvailable();
}

class FakeDeviceCapability implements DeviceCapability {
  @override
  Future<bool> isAvailable() async => true;
}

This is an application-owned example, not generated binding code. Widgets can depend on DeviceCapability while an Android implementation handles the selected Kotlin or Java integration. A browser implementation can return an explicit unsupported result or provide an appropriate web equivalent.

Build a small native spike

Use an isolated branch or sample project. Confirm one call and one failure path before wrapping a large SDK. Keep generator inputs and generation commands in source control so another developer can reproduce the output. Follow the selected tool’s current configuration instead of editing generated bindings by hand.

Record the supported host toolchain, runtime platform and native dependency version. A generator completing successfully does not prove that the shared library is packaged correctly or that every target architecture can load it.

Own the lifecycle

Identify who creates and disposes each native object. Decide which calls are allowed from the UI isolate and which belong on another thread or isolate. Synchronous access does not make an expensive operation safe to run during a frame.

Map native errors into a small domain error type before they reach widgets. The user needs a recovery path such as permission setup or an unavailable-feature message; a raw platform exception is rarely the right UI.

Test at the correct boundary

Use the fake for predictable widget tests. Test the real integration on a supported device and architecture. Include a missing permission, unavailable capability, repeated open/close and app lifecycle transition. Browser compilation is not evidence for a native call.

Exercise

Choose one operation from an existing Kotlin or Java library. Sketch the Dart interface, the native owner and a platform test plan. Explain why you selected a generated native binding or a Pigeon contract instead of choosing from benchmark claims alone.

How this topic connects

Understand first

Apply it next

Lesson progressMark complete after checking the example and exercise.

Find your next idea

Search lessons, code examples and projects.

Type a few words to start exploring.