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;
}
}
Was this page's content helpful?
Thank you for your feedback!
Provide details Thank you for your feedback! Please let us know what we can do to improve.
Provide details Unless stated otherwise, the documentation on this site reflects Dart 3.8.1. Page last updated on 2025-05-08. View source or report an issue.