cflutterdartdart-ffi

Flutter Dart C library binding type error after ffi:gen


I got C library which was generated to binding via ffigen. I need to integrate it to my Flutter app. I added generated .dart file to my project and it has these types:

typedef emetryhub_bluetooth_ctx_t = ffi.Pointer<ffi.Void>;

typedef emetryhub_bluetooth_destroy_cb_t = ffi
    .Pointer<ffi.NativeFunction<ffi.Int32 Function(emetryhub_bluetooth_ctx_t)>>;

external emetryhub_bluetooth_destroy_cb_t destroy_cb;

I am trying to define this C destroy_cb callback function in my Dart code

  Int32 destroyBluetooth(emetryhub_bluetooth_ctx_t ctx) {
    return 0 as Int32;
  }

  bluetooth.ref.destroy_cb = Pointer.fromFunction(destroyBluetooth);

and it gives me an error:

The type 'Int32 Function(Pointer<Void>)' must be a subtype of 'Int32 Function(Pointer<Void>)' for 'fromFunction'.

What am I missing?


Solution

  • Change your Dart callback implementation from:

      Int32 destroyBluetooth(emetryhub_bluetooth_ctx_t ctx) {
        return 0 as Int32;
      }
    

    to

      int destroyBluetooth(emetryhub_bluetooth_ctx_t ctx) {
        return 0;
      }