always_specify_types
Specify type annotations.
此规则自 Dart 2.0 版本起可用。
此规则提供 快速修复 。
_不兼容规则:avoid_types_on_closure_parameters , omit_local_variable_types , omit_obvious_local_variable_types , omit_obvious_property_types _
详情
#From the style guide for the flutter repo:
DO specify type annotations.
Avoid var
when specifying that a type is unknown and short-hands that elide type annotations. Use dynamic
if you are being explicit that the type is unknown. Use Object
if you are being explicit that you want an object that implements ==
and hashCode
.
BAD:
var foo = 10;
final bar = Bar();
const quux = 20;
GOOD:
int foo = 10;
final Bar bar = Bar();
String baz = 'hello';
const int quux = 20;
NOTE: Using the the @optionalTypeArgs
annotation in the meta
package, API authors can special-case type parameters whose type needs to be dynamic but whose declaration should be treated as optional. For example, suppose you have a Key
object whose type parameter you'd like to treat as optional. Using the @optionalTypeArgs
would look like this:
import 'package:meta/meta.dart';
@optionalTypeArgs
class Key<T> {
...
}
void main() {
Key s = Key(); // OK!
}
使用方法
#要启用 always_specify_types
规则,请在你的 analysis_options.yaml
文件中,在 linter > rules 下添加 always_specify_types
:
linter:
rules:
- always_specify_types
除非另有说明,否则本网站上的文档反映的是 Dart 3.6.0。页面最后更新于 2025-02-05。 查看源代码 或 报告问题.