Dart Analyze logoDart Analyze/
DRT-W1401

Instance members can't be accessed from a static methodDRT-W1401

Major severityMajor
Bug Risk categoryBug Risk

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;

  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;

  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;

  static int m(C c) {
    return c.x;
  }
}