跳到主要內容

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 規則,請將 deprecated_member_use_from_same_package 新增到您的 analysis_options.yaml 檔案中 linter > rules 下。

analysis_options.yaml
yaml
linter:
  rules:
    - deprecated_member_use_from_same_package

如果您改為使用 YAML map 語法配置 linter 規則,請在 linter > rules 下新增 deprecated_member_use_from_same_package: true

analysis_options.yaml
yaml
linter:
  rules:
    deprecated_member_use_from_same_package: true