native_field_not_static
Native fields must be static.
Description
#The analyzer produces this diagnostic when an instance field in a class has been annotated with @Native
. Native fields refer to global variables in C, C++ or other native languages, whereas instance fields in Dart are specific to an instance of that class. Hence, native fields must be static.
For more information about FFI, see C interop using dart:ffi.
Example
#The following code produces this diagnostic because the field f
in the class C
is @Native
, but not static
:
import 'dart:ffi';
class C {
@Native<Int>()
external int f;
}
Common fixes
#Either make the field static:
import 'dart:ffi';
class C {
@Native<Int>()
external static int f;
}
Or move it out of a class, in which case no explicit static
modifier is required:
import 'dart:ffi';
class C {
}
@Native<Int>()
external int f;
If you meant to annotate an instance field that should be part of a struct, omit the @Native
annotation:
import 'dart:ffi';
final class C extends Struct {
@Int()
external int f;
}
Unless stated otherwise, the documentation on this site reflects Dart 3.7.3. Page last updated on 2025-05-08. View source or report an issue.