メインコンテンツにスキップ

static_access_to_instance_member

インスタンスメンバー「{0}」には、staticアクセスではアクセスできません。

説明

#

クラス名を使用してインスタンスフィールドにアクセスした場合に、アナライザーはこの診断を生成します。インスタンスフィールドはクラス上には存在せず、クラスのインスタンス上にのみ存在します。

#

以下のコードは、x がインスタンスフィールドであるため、この診断を生成します。

dart
class C {
  static int a = 0;

  int b = 0;
}

int f() => C.b;

一般的な修正

#

staticフィールドにアクセスしたい場合は、フィールド名を既存のstaticフィールドに変更してください。

dart
class C {
  static int a = 0;

  int b = 0;
}

int f() => C.a;

インスタンスフィールドにアクセスしたい場合は、クラスのインスタンスを使用してフィールドにアクセスしてください。

dart
class C {
  static int a = 0;

  int b = 0;
}

int f(C c) => c.b;