flutter-interoperating-with-native-apis by flutter/skills
npx skills add https://github.com/flutter/skills --skill flutter-interoperating-with-native-apisdart:ffi 库。MethodChannel、BasicMessageChannel)。View、iOS UIView)直接嵌入到 Flutter 组件树中的机制。package:web 和 与 JavaScript 和 DOM API 交互的现代、兼容 Wasm 的方法。广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
dart:js_interop使用 FFI 来执行高性能原生代码或利用现有的 C/C++ 库,而无需异步平台通道的开销。
如果要创建标准的 C/C++ 集成(自 Flutter 3.38 起推荐): 使用 package_ffi 模板。此模板利用 build.dart 钩子来编译原生代码,无需特定操作系统的构建文件(CMake、build.gradle、podspec)。
flutter create --template=package_ffi <package_name>
如果需要访问 Flutter 插件 API 或 Play 服务: 使用传统的 plugin_ffi 模板。
flutter create --template=plugin_ffi <plugin_name>
符号可见性: 始终使用 extern "C" 标记 C++ 符号,并防止在链接时优化(LTO)期间链接器丢弃。
extern "C" __attribute__((visibility("default"))) __attribute__((used))
动态库命名(Apple 平台): 确保你的 build.dart 钩子在所有目标架构(例如 arm64 与 x86_64)和 SDK(iphoneos 与 iphonesimulator)上生成完全相同的文件名。不要在 .dylib 或 .framework 名称后附加架构后缀。
绑定生成: 始终使用 package:ffigen 从你的 C 头文件(.h)生成 Dart 绑定。在 ffigen.yaml 中配置此项。
当你需要使用平台的本地语言与平台特定的 API(例如电池、蓝牙、操作系统级服务)交互时,请使用平台通道。
对于复杂或频繁使用的 API,始终优先使用 package:pigeon 而非原始的 MethodChannel 实现。
@HostApi())定义消息协议。makeBackgroundTaskQueue())。Isolate 使用插件/通道,请确保使用 BackgroundIsolateBinaryMessenger.ensureInitialized(rootIsolateToken) 进行注册。使用平台视图将原生 UI 组件(例如 Google 地图、原生视频播放器)嵌入到 Flutter 组件树中。
评估两种渲染模式之间的权衡,并选择合适的一种:
PlatformViewLink + AndroidViewSurface)。这会将原生视图附加到层次结构中,但可能会降低 Flutter 的渲染性能。AndroidView)。这会将原生视图渲染到纹理中。注意:快速滚动可能会丢帧,并且 SurfaceView 存在问题。FlutterPlatformViewFactory 和 FlutterPlatformView。UiKitView 组件。ShaderMask 和 ColorFiltered 组件不能应用于 iOS 平台视图。Flutter Web 支持编译为 WebAssembly (Wasm) 以提高性能并支持多线程。
flutter build web --wasm。Cross-Origin-Embedder-Policy: credentialless(或 require-corp)Cross-Origin-Opener-Policy: same-originpackage:web 和 dart:js_interop。dart:html、dart:js 或 package:js。这些与 Wasm 编译不兼容。HtmlElementView.fromTagName 将任意 HTML 元素(如 <video>)注入到 Flutter Web DOM 中。在绑定到 C/C++ 库时使用此工作流程。
flutter create --template=package_ffi <name>。src/ 目录中。extern "C" 和可见性属性包装。ffigen.yaml 以指向你的头文件。dart run ffigen 以生成 Dart 绑定。hook/build.dart。flutter test -> 查看错误 -> 修复。当你需要从 Dart 调用 Kotlin/Swift API 时使用此工作流程。
pigeon 添加到 dev_dependencies。pigeons/messages.dart 并定义数据类和 @HostApi() 抽象类。MainActivity.kt 或你的插件类中实现生成的接口。AppDelegate.swift 或你的插件类中实现生成的协议。在嵌入原生 UI 组件(例如原生地图或相机视图)时使用此工作流程。
defaultTargetPlatform 为 Android 条件性地返回 AndroidView(或 PlatformViewLink),为 iOS 返回 UiKitView。PlatformView 的类,返回原生的 Android View。PlatformViewFactory 并在 configureFlutterEngine 中注册它。FlutterPlatformView 的类,返回原生的 UIView。FlutterPlatformViewFactory 并在 application:didFinishLaunchingWithOptions: 中注册它。每周安装量
1.7K
代码仓库
GitHub 星标数
784
首次出现
11 天前
安全审计
安装于
codex1.6K
gemini-cli1.6K
opencode1.6K
github-copilot1.6K
kimi-cli1.6K
cursor1.6K
dart:ffi library used to bind Dart directly to native C/C++ APIs.MethodChannel, BasicMessageChannel) connecting the Dart client (UI) to the host platform (Kotlin/Java, Swift/Objective-C, C++).View, iOS UIView) directly into the Flutter widget tree.package:web and dart:js_interop.Use FFI to execute high-performance native code or utilize existing C/C++ libraries without the overhead of asynchronous Platform Channels.
If creating a standard C/C++ integration (Recommended since Flutter 3.38): Use the package_ffi template. This utilizes build.dart hooks to compile native code, eliminating the need for OS-specific build files (CMake, build.gradle, podspec).
flutter create --template=package_ffi <package_name>
If requiring access to the Flutter Plugin API or Play Services: Use the legacy plugin_ffi template.
flutter create --template=plugin_ffi <plugin_name>
Symbol Visibility: Always mark C++ symbols with extern "C" and prevent linker discarding during link-time optimization (LTO).
extern "C" __attribute__((visibility("default"))) __attribute__((used))
Dynamic Library Naming (Apple Platforms): Ensure your build.dart hook produces the exact same filename across all target architectures (e.g., arm64 vs x86_64) and SDKs (iphoneos vs iphonesimulator). Do not append architecture suffixes to the .dylib or .framework names.
Use Platform Channels when you need to interact with platform-specific APIs (e.g., Battery, Bluetooth, OS-level services) using the platform's native language.
Always prefer package:pigeon over raw MethodChannel implementations for complex or frequently used APIs.
@HostApi()).makeBackgroundTaskQueue()).Isolate, ensure it is registered using BackgroundIsolateBinaryMessenger.ensureInitialized(rootIsolateToken).Use Platform Views to embed native UI components (e.g., Google Maps, native video players) into the Flutter widget tree.
Evaluate the trade-offs between the two rendering modes and select the appropriate one:
PlatformViewLink + AndroidViewSurface). This appends the native view to the hierarchy but may reduce Flutter's rendering performance.AndroidView). This renders the native view into a texture. Note: Quick scrolling may drop frames, and SurfaceView is problematic.FlutterPlatformViewFactory and FlutterPlatformView in Swift or Objective-C.UiKitView widget on the Dart side.ShaderMask and ColorFiltered widgets cannot be applied to iOS Platform Views.Flutter Web supports compiling to WebAssembly (Wasm) for improved performance and multi-threading.
flutter build web --wasm.Cross-Origin-Embedder-Policy: credentialless (or require-corp)Cross-Origin-Opener-Policy: same-originpackage:web and dart:js_interop.dart:html, dart:js, or package:js. These are incompatible with Wasm compilation.HtmlElementView.fromTagName to inject arbitrary HTML elements (like <video>) into the Flutter Web DOM.Use this workflow when binding to a C/C++ library.
flutter create --template=package_ffi <name>.src/ directory.extern "C" and visibility attributes.ffigen.yaml to point to your header files.dart run ffigen to generate Dart bindings.hook/build.dart if linking against pre-compiled or system libraries.flutter test -> review errors -> fix.Use this workflow when you need to call Kotlin/Swift APIs from Dart.
pigeon to dev_dependencies.pigeons/messages.dart and define data classes and @HostApi() abstract classes.MainActivity.kt or your Plugin class.AppDelegate.swift or your Plugin class.Use this workflow when embedding a native UI component (e.g., a native map or camera view).
AndroidView (or PlatformViewLink) for Android, and UiKitView for iOS based on defaultTargetPlatform.PlatformView that returns the native Android View.PlatformViewFactory and register it in configureFlutterEngine.FlutterPlatformView that returns the native .Weekly Installs
1.7K
Repository
GitHub Stars
784
First Seen
11 days ago
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
codex1.6K
gemini-cli1.6K
opencode1.6K
github-copilot1.6K
kimi-cli1.6K
cursor1.6K
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
102,200 周安装
Binding Generation: Always use package:ffigen to generate Dart bindings from your C headers (.h). Configure this in ffigen.yaml.
UIViewFlutterPlatformViewFactory and register it in application:didFinishLaunchingWithOptions:.