ffi_ native_ only_ classes_ extending_ nativefieldwrapperclass1_ can_ be_ pointer
Only classes extending NativeFieldWrapperClass1 can be passed as Pointer.
Description
#
The analyzer produces this diagnostic when a function or method annotated
with
@Native
has a parameter in its FFI signature that is a
Pointer
,
but the corresponding Dart parameter type is a class instance that doesn't
extend
NativeFieldWrapperClass1
(or is a Pointer or TypedData).
Example
#
The following code produces this diagnostic because
MyService
doesn't
extend
NativeFieldWrapperClass1
, but the
@Native
signature for its
process
method indicates the receiver should be passed as a
Pointer<Void>
:
import 'dart:ffi';
class MyService { // MyService does not extend NativeFieldWrapperClass1
@Native<Void Function(Pointer<Void>, Int8)>(symbol: 'MyService_process')
external void process(int data);
}
Common fixes
#-
If the Dart class is intended to wrap a native object: Make the Dart class extend
NativeFieldWrapperClass1
. This is the correct approach if the Dart class instance has a corresponding native object whose pointer should be passed.dartimport 'dart:ffi'; class MyService extends NativeFieldWrapperClass1 { @Native<Void Function(Pointer<Void>, Int8)>(symbol: 'MyService_process') external void process(int data); }
-
If you intend to pass an opaque handle to the Dart object: Change the FFI signature in the
@Native
annotation to useHandle
instead ofPointer
for the parameter. This allows passing a reference to the Dart object itself, which native code can interact with using the Dart C API.dartimport 'dart:ffi'; class MyService { @Native<Void Function(Handle, Int8)>(symbol: 'MyService_process') external void process(int data); }
Unless stated otherwise, the documentation on this site reflects Dart 3.9.2. Page last updated on 2025-9-1. View source or report an issue.