dart: void function for ffi

584

Solved it myself.

The working version should be

final void Function() funcNativeStart =
nativeGuestLib
    .lookup<NativeFunction<Void Function()>>("NativeStart")
    .asFunction();

The idea is that since we are "translating" native types into Dart (from right to left as for the equation), we should use the Dart language types on the left side of the equation, and the FFI native types on the right side.

Share:
584
kakyo
Author by

kakyo

The more you learn, the more you hesitate.

Updated on December 20, 2022

Comments

  • kakyo
    kakyo over 1 year

    I'm trying to call a native C/C++ void function form dart side via dart:ffi,

    final Void Function(void) funcNativeStart =
    nativeGuestLib
        .lookup<NativeFunction<Void Function(void)>>("NativeStart")
        .asFunction();
    

    This gives me compiler error

    The type 'Void Function(void)' must be a subtype of 'Void Function(void)' for 'asFunction'
    

    I've played around with a few edits, such as

    final Void Function() funcNativeStart =
    nativeGuestLib
        .lookup<NativeFunction<Void Function()>>("NativeStart")
        .asFunction();
    
    final Void Function(Void) funcNativeStart =
    nativeGuestLib
        .lookup<NativeFunction<Void Function(Void)>>("NativeStart")
        .asFunction();
    

    But the results are all similar to what I've got with the first version.

    How to fix this?