Home > Software engineering >  Using a C DLL in Flutter The type must be a subtype
Using a C DLL in Flutter The type must be a subtype

Time:07-22

dll` to my flutter project

In .dll I type function:

int testdll(int param) {
//...
}

on flutter I type this:

final DynamicLibrary nativePointerTestLib = DynamicLibrary.open("assets/SimpleDllFlutter.dll");

final Int Function(Int arr) testdllflutter =
nativePointerTestLib
    .lookup<NativeFunction<Int Function(Int)>>("testdll")
    .asFunction();

and I get error

The type 'Int Function(Int)' must be a subtype of 'Int Function(Int)' for 'asFunction'. (Documentation) Int is defined in C:\flutter\bin\cache\pkg\sky_engine\lib\ffi\c_type.dart (c_type.dart:77). Int is defined in C:\flutter\bin\cache\pkg\sky_engine\lib\ffi\c_type.dart (c_type.dart:77). Try changing one or both of the type arguments.

Do you have any ideas?

CodePudding user response:

I try call wrond types, correct types:

final int Function(int arr) testdllflutter =
nativePointerTestLib
    .lookup<NativeFunction<Int32 Function(Int32)>>("testdll")
    .asFunction();

and it works

CodePudding user response:

You are mixing up Dart integers (int) with C integers (IntXX). You are also using the old lookup, where the newer lookupFunction saves you some boilerplate.

Use this instead:

  final int Function(int param) testdllflutter = nativePointerTestLib
      .lookupFunction<Int32 Function(Int32), int Function(int)>('testdll');

testdllflutter is now a function taking one int returning int. The two types you need to pass to lookupFunction should be, first, the native-styled one, using native types that match the actual C integer sizes; then, second, the Dart-styled one, using Dart types. (The second one should match the type of the variable you are assigning it to.)

  • Related