You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ffigen生成C库绑定后Flutter Dart回调函数类型错误求助

问题原因及解决办法

这个错误看似矛盾,本质是Dart FFI的Pointer.fromFunction对类型匹配有严格要求——它无法识别你自定义的emetryhub_bluetooth_ctx_t类型别名,必须直接使用原始的ffi.Pointer<Void>作为参数类型。

修改你的Dart回调函数代码:

import 'dart:ffi';

// 直接使用Pointer<Void>作为参数类型
int destroyBluetooth(Pointer<Void> ctx) {
  return 0;
}

// 赋值时传入函数,同时指定默认返回值(可选但推荐)
bluetooth.ref.destroy_cb = Pointer.fromFunction(destroyBluetooth, 0);

额外注意事项:

  • 确保所有FFI相关类型(Pointer、NativeFunction等)都来自dart:ffi包,不要混用其他第三方包的同名类型。
  • 如果你需要在函数内使用自定义的emetryhub_bluetooth_ctx_t类型,可以在函数内部做类型转换:
    int destroyBluetooth(Pointer<Void> ctx) {
      final typedCtx = ctx as emetryhub_bluetooth_ctx_t;
      // 后续逻辑基于typedCtx处理
      return 0;
    }
    
  • Pointer.fromFunction的第二个参数是默认返回值,当回调无法正常执行时会返回该值,建议显式指定以避免潜在的未定义行为。

内容的提问来源于stack exchange,提问作者Kęstutis Ramulionis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 07:54:55