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:
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:
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:
class C {
int x = 0;
static int m(C c) {
return c.x;
}
}
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.