always_specify_types
Specify type annotations.
This rule is available as of Dart 2.0.
This rule has a quick fix available.
Incompatible rules: avoid_types_on_closure_parameters, omit_local_variable_types, omit_obvious_local_variable_types, omit_obvious_property_types
Details
#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!
}
Usage
#To enable the always_specify_types
rule,
add always_specify_types
under linter > rules in your
analysis_options.yaml
file:
linter:
rules:
- always_specify_types
除非另有说明,文档之所提及适用于 Dart 3.5.4 版本,本页面最后更新时间: 2024-08-02。 查看文档源码 或者 报告页面问题。