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
除非另有说明,否则本网站上的文档反映的是 Dart 3.7.1 版本。页面上次更新于 2025-03-07。 查看源代码 或 报告问题。