Dart Analyze logoDart Analyze/
DRT-W1498

Function type must be a subtype of the type argument in FFI invocationDRT-W1498

Major severityMajor
Bug Risk categoryBug Risk

The analyzer produces this diagnostic in two cases:

  • In an invocation of Pointer.fromFunction, or a NativeCallable constructor where the type argument (whether explicit or inferred) isn't a supertype of the type of the function passed as the first argument to the method.
  • In an invocation of DynamicLibrary.lookupFunction where the first type argument isn't a supertype of the second type argument.

For more information about FFI, see C interop using dart:ffi.

Example

The following code produces this diagnostic because the type of the function f (String Function(int)) isn't a subtype of the type argument T (Int8 Function(Int8)):

import 'dart:ffi';

typedef T = Int8 Function(Int8);

double f(double i) => i;

void g() {
  Pointer.fromFunction<T>(f, 5.0);
}

Common fixes

If the function is correct, then change the type argument to match:

import 'dart:ffi';

typedef T = Float Function(Float);

double f(double i) => i;

void g() {
  Pointer.fromFunction<T>(f, 5.0);
}

If the type argument is correct, then change the function to match:

import 'dart:ffi';

typedef T = Int8 Function(Int8);

int f(int i) => i;

void g() {
  Pointer.fromFunction<T>(f, 5);
}