Skip to main content

avoid-explicit-type-declaration

added in: 1.1.0
🛠
Pro+

Warns when a variable is declared with an explicit type that can be omitted.

Adding an explicit type annotation to variables with initializers is usually unnecessary, since the type can be inferred from the initializer.

Example

❌ Bad:

class SomeClass {
// LINT: Avoid explicit variable type declaration. Try removing the type.
static const String val = '123';

// LINT: Avoid explicit variable type declaration. Try removing the type.
final Map<String, String> initedWithType = <String, String>{};

// LINT: Avoid explicit variable type declaration. Try removing the type.
final Set<String> elements = {};

// LINT: Avoid explicit variable type declaration. Try removing the type.
final (String,) record = ('hello',);
}

✅ Good:

class SomeClass {
static const val = '123';

final initedWithType = <String, String>{};

final elements = <String>{};

final record = ('hello',);

late final (String,) lazyRecord;
}