dart-use-ffigen
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.
Security Assessment
About dart-use-ffigen
Guides agents to generate Dart FFI bindings with package:ffigen (the FfiGenerator API) instead of writing them by hand. Hand-written bindings are described as error-prone and brittle, so whenever native headers exist or are produced by a build step, manual DynamicLibrary.lookup calls, @Native external functions, and raw struct classes should be avoided in favor of generated code. The skill establishes conventions for where generator scripts, headers, and outputs live, and how to filter what gets generated.
The workflow places the generator script at tool/ffigen.dart within the target package, resolving paths relative to Platform.script so it runs from any working directory. Third-party headers belong in third_party/ (or a src/ directory at the package root), and generated bindings that reference third-party headers must go under lib/src/third_party/ with the primary file using a .g.dart extension. Dependencies are added with dart pub add dev:ffigen, the script is run via dart run tool/ffigen.dart, and the result is verified with dart analyze. The generator runs the Dart formatter automatically when format is set to true.
Configuration is programmatic through objects imported from package:ffigen/ffigen.dart. FfiGenerator orchestrates parsing and generation and accepts Headers, Enums, Functions, Globals, Integers, Macros, Structs, Typedefs, Unions, UnnamedEnums, an optional ObjectiveC, and a required Output. Headers configures entryPoints, an include filter for transitive headers, compilerOptions, and ignoreSourceErrors. Functions selects exposed symbols via include or Functions.includeSet, with isLeaf and recordUse options. Output sets dartFile, recordUseMapping, preamble, and format. Important constraints include using targeted inclusion filters rather than importing an entire library, supplying a preamble with the matching license, the native copyright header, and an auto-generation warning, and enabling recordUse plus a recordUseMapping .g.dart file when bindings participate in native asset tree shaking. Style lints in generated files are suppressed via ignore_for_file in the preamble; genuine compilation errors should be reported upstream rather than edited manually.
FAQ
When should I use ffigen instead of writing FFI bindings by hand?
Whenever native header files exist or are generated by a build step. Hand-written bindings are error-prone and brittle, so manual DynamicLibrary.lookup, @Native external functions, and raw struct classes should be replaced by FfiGenerator output.
Where should the generator script and generated files live?
The generator script goes at tool/ffigen.dart within the target package. Generated bindings that interface with a third-party library must be placed under lib/src/third_party/, and the primary bindings file must use the .g.dart extension, for example sqlite3.g.dart.
How do I add ffigen as a dependency?
Add it to dev_dependencies, for example with the CLI command dart pub add dev:ffigen. You can check the latest version on pub.dev, such as the example caret constraint shown in the doc.
How do I limit which native symbols get generated?
Apply precise positive inclusion filters rather than importing an entire library. Use matcher functions or helpers like Functions.includeSet to select only the functions you need, which minimizes the size and cognitive load of the generated code.
What should I do if dart analyze reports problems in the generated file?
For style or lint warnings, append the warning codes to the ignore_for_file list in the preamble configuration rather than editing the file. For actual compilation or analysis errors, do not edit the generated file manually; report the details to the user so they can file an issue on the dart-lang/native repository.
Install dart-use-ffigen
Quick Setup:
- Copy the skill folder to
.claude/skills/ - Claude will automatically detect and use the skill
Repository
dart-lang/skills