strict_top_level_inference
Specify type annotations.
This rule is currently experimental and not yet available in a stable SDK.
This rule has a quick fix available.
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;
Usage
#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
Unless stated otherwise, the documentation on this site reflects Dart 3.6.0. Page last updated on 2024-07-03. View source or report an issue.