跳到主要內容

deprecated_member_use_from_same_package

穩定
提供修正

避免從宣告已棄用元素的套件內部使用這些元素。

詳細資訊

#

標註 @Deprecated 的元素不應從宣告它們的套件內部被參考。

避免 使用已棄用的元素。

...

不良範例

dart
// Declared in one library:
class Foo {
  @Deprecated("Use 'm2' instead")
  void m1() {}

  void m2({
      @Deprecated('This is an old parameter') int? p,
  })
}

@Deprecated('Do not use')
int x = 0;

// In the same or another library, but within the same package:
void m(Foo foo) {
  foo.m1();
  foo.m2(p: 7);
  x = 1;
}

已棄用的元素可以從其他已棄用的元素內部使用,以便允許將一組 API 作為一個單元一起棄用。

良好範例

dart
// Declared in one library:
class Foo {
  @Deprecated("Use 'm2' instead")
  void m1() {}

  void m2({
      @Deprecated('This is an old parameter') int? p,
  })
}

@Deprecated('Do not use')
int x = 0;

// In the same or another library, but within the same package:
@Deprecated('Do not use')
void m(Foo foo) {
  foo.m1();
  foo.m2(p: 7);
  x = 1;
}

啟用

#

若要啟用 deprecated_member_use_from_same_package 規則,請在您的 analysis_options.yaml 檔案中的 linter > rules 下新增 deprecated_member_use_from_same_package

analysis_options.yaml
yaml
linter:
  rules:
    - deprecated_member_use_from_same_package

如果您改為使用 YAML 對應語法來設定 linter 規則,請在 linter > rules 下新增 deprecated_member_use_from_same_package: true

analysis_options.yaml
yaml
linter:
  rules:
    deprecated_member_use_from_same_package: true