avoid-type-to-string
Same as avoid_type_to_string in Dart lints
- Group
- Correctness
- Path
lint/correctness/avoid-type-to-string- Default
- On (recommended)
- Origin
- Dart lints
- Rule
- 148 of 512
Avoid Type.toString() in production code, where type names may be minified.
Web and AOT builds may minify or strip type names, so a string built from a
Type can differ between debug and release:
class TypeName implements Type {}
final name = TypeName().toString(); // BAD: uses `Object.toString`
if (value is Widget) {} // GOOD: compare types, not their names
Reports calls to or
tear-offs of Object.toString when the target has a non-nullable subtype of
dart:core Type. The target is usually the receiver's static interface
type; calls without an interface-typed receiver can be affected by the
enclosing declaration. A tear-off through an identifier prefix is checked
only when that prefix has an interface type.
Type itself (and so every runtimeType read) is never reported, because
dart:core Type redeclares toString.
Invalid
void foo(String Function() f) {}
class Type2 implements Type {}
class Type3 extends Type2 {}
var direct = Type2().toString();
var inherited = Type3().toString();
class Type4 implements Type {
late var explicitThis = this.toString();
late var implicitThis = toString();
late var viaSuper = super.toString();
void tearOffs() {
foo(this.toString);
foo(toString);
}
}
extension E on Type2 {
String describe() => toString();
} var direct = Type2().toString(); var inherited = Type3().toString();class Type4 implements Type { late var explicitThis = this.toString(); late var explicitThis = this.toString(); late var implicitThis = toString(); late var implicitThis = toString(); late var viaSuper = super.toString(); void tearOffs() { foo(this.toString); foo(this.toString); foo(toString);extension E on Type2 { String describe() => toString();Valid
mixin M {
@override
String toString() => '';
}
class Type2 with M implements Type {
String get x => toString();
}
class Plain {
String describe() => toString();
}
class Named {
@override
String toString() => 'Named';
}
void compareTypes(Object value) {
if (value.runtimeType == int) {
print('int');
}
}
void otherToString(int number, Plain plain) {
print(number.toString());
print(plain.toString());
print(Named().toString());
}
void interpolation(Type type) {
print('$type');
}
void dynamicReceiver(dynamic value) {
print(value.toString());
}
// `dart:core` `Type` redeclares `toString`, so `Type` receivers (including
// every `runtimeType` read) never resolve to `Object.toString`.
var fromRuntimeType = 7.runtimeType.toString();
void tearOff(void Function(String Function()) foo) {
foo(7.runtimeType.toString);
}
extension E on Type {
String describe() => toString();
}
class Widget {
String label() => runtimeType.toString();
}
void typeParameter(Type type) {
print(type.toString());
}Configure avoid-type-to-string
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"correctness": {
"avoid-type-to-string": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/correctness/avoid-type-to-string: <reason>More correctness rules
- avoid-uncaught-future-errorsrecommended
Flags local futures first awaited after another await inside a try/catch.
- avoid-unconditional-break
Reports unconditional break, continue, return or throw in a for-loop body.
- avoid-undisposed-instancesrecommendedflutter
Warns when a disposable constructor result is discarded.
- avoid-unmodified-loop-conditionrecommended
Flags loop conditions whose local scalar inputs are never modified by the loop.
- avoid-unnecessary-setstaterecommendedflutter
Avoid setState during build, initState, or didUpdateWidget.
- avoid-unnecessary-stateful-widgetsrecommendedflutter
Warns when a
StatefulWidget's local State contains only a pure build method. - avoid-unreachable-for-loop
Reports iteration of a collection in a branch that requires it to be empty.
- avoid-unrelated-type-castsrecommended
Flags casts between types proven unrelated by canonical type resolution.