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;
}
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。