must_be_a_subtype
The type '{0}' must be a subtype of '{1}' for '{2}'.
Description
#The analyzer produces this diagnostic in two cases:
- In an invocation of
Pointer.fromFunction
, or aNativeCallable
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)
):
dart
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:
dart
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:
dart
import 'dart:ffi';
typedef T = Int8 Function(Int8);
int f(int i) => i;
void g() {
Pointer.fromFunction<T>(f, 5);
}
Was this page's content helpful?
Thank you for your feedback!
Provide details Thank you for your feedback! Please let us know what we can do to improve.
Provide details Unless stated otherwise, the documentation on this site reflects Dart 3.8.1. Page last updated on 2025-05-08. View source or report an issue.