네이티브 함수 타입이어야 합니다

네이티브 함수 타입이어야 합니다

The type '{0}' given to '{1}' must be a valid 'dart:ffi' native function type.

이 진단은 Pointer.fromFunction, DynamicLibrary.lookupFunction, 또는 NativeCallable 생성자 호출에 전달한 타입 인자가 유효한 dart:ffi 네이티브 함수 타입이 아닐 때 나타나요.

출처: must_be_a_native_function_type

본문

이 애널라이저 진단은 Pointer.fromFunction, DynamicLibrary.lookupFunction, 또는 NativeCallable 생성자 중 하나를 호출할 때, 타입 인자(명시적이든 추론된 것이든)가 유효한 dart:ffi 네이티브 함수 타입이 아닌 경우에 발생해요.

FFI에 대한 자세한 내용은 C interop using dart:ffi 문서를 참고하면 좋아요.

예시

다음 코드는 타입 TFunction의 어떤 서브클래스든 될 수 있는데, fromFunction의 타입 인자는 네이티브 함수 타입이어야 하기 때문에 이 진단이 나와요.

import 'dart:ffi';

int f(int i) => i * 2;

class C<T extends Function> {
  void g() {
    Pointer.fromFunction<T>(f, 0);
  }
}

일반적인 해결 방법

호출에 쓸 타입 인자로 네이티브 함수 타입을 사용하면 돼요.

import 'dart:ffi';

int f(int i) => i * 2;

class C<T extends Function> {
  void g() {
    Pointer.fromFunction<Int32 Function(Int32)>(f, 0);
  }
}

더 알아보기