A document scanner is a useful way to think about native integration. The Flutter screen owns the capture button, progress and review flow. A platform library owns recognition. Between them sits a boundary that needs to return useful data, handle cancellation and survive the user leaving the screen.
Flutter 3.50, released on November 11, makes direct native interop a more practical option. Objective-C support in FFIgen and Java/Kotlin support in JNIgen are stable. Pigeon's direct interop mode remains a preview. That distinction matters when deciding which parts of a production application to change. See the official release announcement.
This guide proposes a way to design and evaluate that scanner boundary. It does not claim a device benchmark or provide a complete scanner implementation.
Choose the shape of the boundary first
The native interop overview describes two approaches. FFIgen and JNIgen generate bindings from an existing native API. Pigeon starts with a Dart contract and generates matching interfaces. Direct bindings require attention to native ownership and threading; Pigeon can also continue using platform channels.
For a scanner, ask what you already have. If there is an established Android library with a large API surface, generating bindings might save repetitive integration code. If the feature needs one small operation implemented differently on each platform, an intentional cross-platform contract may be easier to maintain.
| Your starting point | Candidate | Question to answer before adoption |
|---|---|---|
| Existing C or Objective-C library | FFIgen | Can the exposed API be wrapped behind a small Dart interface? |
| Existing Java or Kotlin library | JNIgen | What thread and lifecycle does each operation require? |
| One shared contract, separate host implementations | Pigeon | Is its established channel mode sufficient, or is the preview justified? |
| A working integration without a measured problem | Keep it while evaluating | What user-visible issue would a rewrite solve? |
The generator should follow the architecture decision. It should not make that decision for you.
Keep generated classes away from widgets
A scanner screen should not need to understand JNI object references or the naming conventions of an Apple framework. Put those details behind a feature interface. The following is an illustrative application contract, not generated binding code:
class ScanResult {
const ScanResult({required this.text, required this.pageCount});
final String text;
final int pageCount;
}
abstract interface class DocumentScanner {
Future<ScanResult> scanFile(String path);
}
This gives the presentation layer a stable vocabulary. An Android adapter can map the native result into ScanResult; an Apple adapter can make the same mapping independently. Tests can supply a fake scanner without loading a native library.
Decide what a cancelled scan means before implementation. It could be a distinct result, a documented exception, or a separate operation handle. Avoid letting one platform return an empty string while the other throws: the UI then needs platform knowledge to interpret the same user action.
Treat ownership as part of the API design
FFIgen's guide covers generated bindings for C and Objective-C. JNIgen's guide covers Java and Kotlin integrations. Follow their setup and lifetime guidance for the particular library you bind.
For the scanner example, write down the answers to four questions during review:
- Who owns the source image while recognition runs?
- Which object retains the recognition callback?
- What happens if the screen closes before completion?
- When can temporary files and native resources be released?
A useful engineering exercise is to draw a timeline with capture, recognition, cancellation and disposal. Any resource without a clear end to its lifetime deserves a closer look. Generating correct method signatures does not, by itself, answer these application questions.
A synchronous call can still be expensive
Direct interop can remove message serialization, but that does not establish that the native operation is cheap. Treat binding overhead, native processing time, data conversion and UI updates as separate costs.
For example, a tiny metadata lookup and full-page text recognition should not share the same performance assumptions. Decide where recognition executes from the native library's documented threading rules. Do not move a call to a background isolate simply because it compiles there; first confirm that the library and every object involved support that execution model.
Record errors in a vocabulary the feature understands: unavailable capability, permission denied, unsupported input, cancelled operation and processing failure. Keep detailed native diagnostics in development logs while returning useful recovery actions to the screen.
Measure a representative operation
A sensible comparison uses the same image set, device, build mode and output validation for both integrations. Measure the whole operation as well as time at the boundary. Include a large document and repeated runs, because a quick first result can hide growing memory usage.
Also try leaving the screen during recognition, denying permission, rotating the device and starting another scan immediately. These are proposed acceptance checks, not results from testing this example.
Adopt direct interop where it makes the integration clearer or solves a measured bottleneck. Keep the feature contract small enough that the binding strategy can change without redesigning the scanner screen.
Continue the Flutter 3.50 series
Pair this guide with Android platform-view testing when the native library also supplies a view, or revisit our repository boundary example for the surrounding application structure.