31 skills · 85 min
Skills
Skill 11 of 31
Guide agents to use package:ffigen to automatically generate FFI bindings instead of writing them manually.
4 minutes · 929 words · 21 sections
Install
npx skills add flutter/agent-plugins --skill dart-use-ffigennpx skills add flutter/agent-plugins/plugin marketplace add flutter/agent-pluginsThe first command installs just this skill, by the name in its SKILL.md; the second installs the whole repository.
Automate and standardize the generation of FFI bindings using package:ffigen (FfiGenerator). Writing FFI bindings by hand is error-prone, brittle, and highly discouraged.
.h files) exist or are generated by a build step, never write manual DynamicLibrary.lookup, @Native external functions, or raw struct classes. Always use FfiGenerator to generate them.tool/ffigen.dart within the target package root.third_party/ within the target package (otherwise placing them in a src/ directory at the package root is also acceptable). If the headers are not in one of these standard locations, notify the user that it would be cleaner to move the header files to the standard location (e.g., third_party/).Functions.includeSet or filtering matches in include closures).lib/src/third_party/. The primary generated FFI bindings file must strictly use the .g.dart extension (e.g. sqlite3.g.dart).preamble in the Output class to specify the license. This must match the native third-party library’s license, explicitly include the copyright header of the target native header file, and contain an automatic generation warning (e.g. // Generated by package:ffigen. Do not edit manually.).dart analyze.recordUse: (_) => true under Functions.recordUseMapping target in Output (which must strictly be a .g.dart file under lib/src/third_party/, e.g. ) to register bindings for symbol tree shaking.To construct the programmatic generator, use the core configuration objects imported from package:ffigen/ffigen.dart:
FfiGeneratorThe parent class that orchestrates the configuration, parsing, and code generation.
FfiGenerator({
Headers headers = const Headers(),
Enums enums = Enums.excludeAll,
Functions functions = Functions.excludeAll,
Globals globals = Globals.excludeAll,
Integers integers = const Integers(),
Macros macros
HeadersConfigures Clang header parsing targets and compiler flags.
entryPoints: A list of target header Uri inputs.include: A filter function bool Function(Uri header) that handles transitive header imports.compilerOptions: Custom preprocessor/include compiler flags to pass directly to libclang.ignoreSourceErrors: Set to true to silence errors occurring inside third-party headers during parsing.FunctionsSpecifies which native C/C++ functions to expose in Dart.
include: A matcher function (e.g. (decl) => {'my_func'}.contains(decl.originalName) or Functions.includeSet({'my_func'})).isLeaf: Declares functions as leaf functions ((decl) => true) if they do not call back into Dart or block thread execution.recordUse: Enables metadata generation for native asset tree shaking (essential in dart-lang/native). Set to (_) => true.OutputConfigures target generated files.
dartFile: Target Uri where the primary FFI bindings will be written.recordUseMapping: Target Uri for recorded usage metadata maps (crucial for linking-time tree shaking).preamble: Text inserted at the top of the generated file (licensing, annotations).format: Set to true to run the Dart formatter automatically.Open the package’s pubspec.yaml and verify the dev_dependencies contains ffigen. Use the Dart MCP server or look up the latest version on pub.dev (opens in a new tab) (e.g., ^20.1.1).
You can add it automatically using the CLI:
dart pub add dev:ffigenCreate a programmatic generator script under the package’s tool/ directory (e.g., tool/ffigen.dart).
Resolve paths relative to Platform.script to make sure it runs successfully from any working directory:
final packageRoot = Platform.script.resolve('../');
final headerFile = packageRoot.resolve('third_party/library.h');
final targetBindings = packageRoot.resolve('lib/src/third_party/bindings.g.dart');tool/ffigen.dart)Define void main() and run FfiGenerator with dynamic options (see complete example below).
Execute the script from the terminal inside the target package folder:
dart run tool/ffigen.dartVerify that the generated bindings are correct and resolve any analysis issues. FFIgen automatically runs the Dart formatter on the output file (via format: true configuration), so manual formatting is not required.
dart analyzedart analyze reports style or lint warnings inside the generated file, append the corresponding warning codes to the ignore_for_file: list in your generator script’s preamble configuration (e.g., adding camel_case_types, non_constant_identifier_names, etc.). Do not modify the package’s global rules.dart analyze reports actual compiler or analysis errors (not warnings) inside the generated file, do not attempt to edit the generated file manually. Report these error details directly to the user so they can file an issue on the repository at github.com/dart-lang/native (opens in a new tab).Let’s assume we are working with the SQLite package under pkgs/code_assets/example/sqlite, which embeds SQLite C library sources inside third_party/sqlite/ and accesses it via FFI.
third_party/sqlite/sqlite3.h)// The author disclaims copyright to this source code.
#ifndef SQLITE3_H_
#define SQLITE3_H_
const char *sqlite3_libversion(void);
#endif // SQLITE3_H_A developer might attempt to handcraft this integration. It is fragile, blocks tree-shaking metadata, and is highly prone to ABI and structural mapping issues:
// lib/src/sqlite3_manual.dart
import 'dart:ffi' as ffi;
import 'package:ffi/ffi.dart';
// Flaw 1: Hardcoded DynamicLibrary lookup blocks integration with modern native asset compilation.
final ffi.DynamicLibrary _dylib = ffi.DynamicLibrary.open('libsqlite3.so');
// Flaw 2: Manual function type matching requires writing redundant dynamic lookup boilerplate and lacks tree-shaking metadata.
typedef _sqlite3_libversion_C
Create a programmatic script at tool/ffigen.dart:
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:io';
import 'package:ffigen/ffigen.dart';
void main() {
// Resolve paths dynamically relative to Platform.script
final
Run this in the package root directory:
dart run tool/ffigen.dartThis will automatically create:
lib/src/third_party/sqlite3.g.dartlib/src/third_party/sqlite3.record_use_mapping.g.dartAlways perform the following verification before completing a binding generation task:
lib/src/third_party/ (required for third-party licensed code) and the primary FFI bindings file strictly uses the .g.dart extension.dart analyze and ensure there are zero compiler/analyzer errors or warnings in the package.
ignore_for_file rules to the generator’s preamble configuration (do not modify global package rules).Guide agents to use `package:ffigen` to automatically generate FFI bindings instead of writing them manually. Use this skill when a task involves writing new FFI bindings, extending C/Objective-C/Swift integrations, or replacing hand-crafted `dart:ffi` setups.
The verbatim description from this skill’s front matter — the string an agent matches on to decide whether to load it.
main, last pushed 17 September 2026.SKILL.md, not by matching a directory convention. 2 distinct layouts observed: .agents/agents/reidbaker-agent/skills/*/SKILL.md, skills/*/SKILL.md.h1 and no skipped levels:.claude-plugin/marketplace.json by Dart and Flutter Team, declaring 1 plugin. It is read for editorial metadata only — never as the skill index, which is always the repository tree.lib/src/third_party/sqlite3.record_use_mapping.g.dart/flutter/agent-plugins.md, and each skill at its own .md URL.