Flutter Dart C library binding type error after ffi:gen

25 Views Asked by At

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?

1

There are 1 best solutions below

0
Richard Heap On BEST ANSWER

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;
  }