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?
Your
handleStateEventfunction 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 seereturn UnsignedInt();since you can't instantiate anUnsignedIntyourself.Change your method to:
When you implement the body of the function, just use the 3
intparameters naturally. AccessinContextthe normal way forPointers