Native integration can reuse an existing C library or platform API, but the Dart type system cannot automatically guarantee the lifetime of memory owned outside Dart. Keep that boundary small and document who allocates and releases each resource.
Start with a portable adapter
Expose a domain operation such as Future<Uint8List> encodeReport(...) rather than leaking raw native pointers into widgets. A Uint8List is a typed byte buffer useful for binary protocols and files; it is not a parsed text string. Decode text with the appropriate character encoding.
import 'dart:convert';
import 'dart:typed_data';
void main() {
final bytes = Uint8List.fromList(utf8.encode('Invoice ready'));
print(bytes.length);
print(utf8.decode(bytes));
}
This byte-buffer example is portable and needs no native library. A real FFI example additionally needs a compiled library, matching signatures and target-architecture packaging.
Choose a binding tool
FFIgen can generate bindings from supported headers; JNIgen targets Java/Kotlin integration. Pigeon provides a schema-based cross-platform contract with its own channel and native-interop modes. Use the native interoperability lesson for the current stability and tooling distinction.
Track lifetime and thread constraints
A pointer to freed memory is not repaired by a Dart null check. Define disposal, callback lifetime and which thread/isolate may invoke the native API. Validate buffer lengths before interpreting binary data, and do not retain a borrowed pointer after its owner releases it.
New build hooks affect how native dependencies are built and packaged. Check the package’s current build instructions before choosing a compile command. Test the actual CPU architecture and release artifact; a local language example does not verify a native plugin.
Exercise
Design an adapter for an existing native encoder, including ownership and error conversion. Write a fake for application tests. List the additional build and runtime checks needed before claiming the native implementation works on Linux, Windows or a mobile platform.