strict_top_level_inference
Specify type annotations.
Details
#Do type annotate top-level and class-like member declarations, where types are not inferred from super-interfaces or initializers.
The lint warns about every omitted return type, parameter type, and variable type of a top-level declaration or class-like-namespace-level declaration (static or instance member or constructor declaration), which is not given a type by inference, and which therefore defaults to dynamic.
The only omitted types that can be given a type by top-level inference, are those of variable declarations with initializer expressions, and return and parameter types of instance members that override a consistent combined super-interface signature.
Setters do not need a return type, as it is always assumed to be void
.
BAD:
var _zeroPointCache;
class Point {
get zero => ...;
final x, y;
Point(x, y) {}
closest(b, c) => distance(b) <= distance(c) ? b : c;
distance(other) => ...;
}
_sq(v) => v * v;
GOOD:
Point? _zeroPointCache;
class Point {
Point get zero => ...;
final int x, y;
Point(int x, int y) {}
closest(Point b, Point c) =>
distance(b) <= distance(c) ? b : c;
distance(Point other) => ...;
}
int _sq(int v) => v * v;
Enable
#To enable the strict_top_level_inference
rule,
add strict_top_level_inference
under linter > rules in your
analysis_options.yaml
file:
linter:
rules:
- strict_top_level_inference
If you're instead using the YAML map syntax to configure linter rules,
add strict_top_level_inference: true
under linter > rules:
linter:
rules:
strict_top_level_inference: true
除非另有说明,文档之所提及适用于 Dart 3.7.1 版本,本页面最后更新时间: 2025-01-27。 查看文档源码 或者 报告页面问题。