Dart ffi NativeCallack must be of subtype

30 Views Asked by At

I'm working on a Dart project that needs to call a dylib in Linux and receive a callback via a function pointer. All functions that call into the library are working as expected but I can't get the callback to work and there is very sparse information available to help find the problem.

typedef EventHandlerFunction = UnsignedInt Function(UnsignedInt inEvent, UnsignedInt inPropertyID, UnsignedInt inParam, ffi.Pointer<Void> inContext); 

UnsignedInt handleStateEvent(UnsignedInt inEvent, UnsignedInt inPropertyID, UnsignedInt inParam, ffi.Pointer<Void> inContext) {

  return const UnsignedInt();
}

final callback = NativeCallable<EventHandlerFunction>.isolateLocal(handleStateEvent, exceptionalReturn: 0);

This appears to match the only examples I've found but I get the error:

The type 'UnsignedInt Function(UnsignedInt, UnsignedInt, UnsignedInt, Pointer<Void>)' must be a subtype of 'UnsignedInt Function(UnsignedInt, UnsignedInt, UnsignedInt, Pointer<Void>)' for 'NativeCallable'

The required type matches the specified type so I'm stumped as to what the problem is. Can anyone shed any light on this?

1

There are 1 best solutions below

0
Richard Heap On

Your handleStateEvent function needs to look like a Dart function with real Dart types. (It can't have the 'dummy' native types since they aren't real Dart types. In particular, you get a clue that it's wrong when you see return UnsignedInt(); since you can't instantiate an UnsignedInt yourself.

Change your method to:

int handleStateEvent(
  int inEvent,
  int inPropertyID,
  int inParam,
  Pointer<Void> inContext,
) {
  return 123;
}

When you implement the body of the function, just use the 3 int parameters naturally. Access inContext the normal way for Pointers