instance_member_access_from_static
Instance members can't be accessed from a static method.
Description
#The analyzer produces this diagnostic when a static method contains an unqualified reference to an instance member.
Example
#The following code produces this diagnostic because the instance field x
is being referenced in a static method:
dart
class C {
int x = 0;
static int m() {
return x;
}
}
Common fixes
#If the method must reference the instance member, then it can't be static, so remove the keyword:
dart
class C {
int x = 0;
int m() {
return x;
}
}
If the method can't be made an instance method, then add a parameter so that an instance of the class can be passed in:
dart
class C {
int x = 0;
static int m(C c) {
return c.x;
}
}
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。