avoid_as
Avoid using as
.
此规则已从最新的 Dart 版本中移除。
详情
#NOTE: This rule was removed from the SDK in Dart 3; it is no longer functional. Its advice is compiler-specific and mostly obsolete with null safety.
AVOID using as
.
If you know the type is correct, use an assertion or assign to a more narrowly-typed variable (this avoids the type check in release mode; as
is not compiled out in release mode). If you don't know whether the type is correct, check using is
(this avoids the exception that as
raises).
BAD:
(pm as Person).firstName = 'Seth';
GOOD:
if (pm is Person)
pm.firstName = 'Seth';
but certainly not
BAD:
try {
(pm as Person).firstName = 'Seth';
} on CastError { }
Note that an exception is made in the case of dynamic
since the cast has no performance impact.
OK:
HasScrollDirection scrollable = renderObject as dynamic;
使用方法
#要启用 avoid_as
规则,请在你的 analysis_options.yaml
文件中,在 linter > rules 下添加 avoid_as
:
linter:
rules:
- avoid_as
除非另有说明,否则本网站上的文档反映的是 Dart 3.6.0。页面最后更新于 2025-02-05。 查看源代码 或 报告问题.