default_to_string
Reports a value made text whose class has no toString of its own, so it shows as “Instance of ‘Order’”. Give the class a toString.
- Tier
- advisory
- Scope
- Dart
- Lowest preset
- recommended
- Fix
- none
What it reports
Section titled “What it reports”A value made text whose text is “Instance of ‘Order’”, in one of three places:
- a call of
toString()on it; - an interpolation of it in the first argument of a Flutter
Textwidget:Text('$order'); - an interpolation of it in a
toString:'Cart($order)'.
The value’s class is neither abstract nor sealed, and neither it nor a class or a mixin above it declares a toString. A nullable value counts by its class.
It does not report:
- an enum, a record, a function, a type parameter or
Objectitself; - an abstract or a sealed class, which may stand for a subclass with a
toString; - a call of
toString()onthis; - an interpolation anywhere else, where the default text is often enough.
A concrete class that declares no toString is reported also where its subclasses declare one.
The text is shown to a user in a widget, or handed by an outer toString to every log that prints it, and it says only which class the value is. An explicit toString() says the author wanted text of the value.
Example
Section titled “Example”class Order { Order(this.id);
final int id;}
Text('Order: $order')class Order { Order(this.id);
final int id;
@override String toString() => 'Order $id';}
Text('Order: $order')Rule parameters
Section titled “Rule parameters”Set under the assay: key of the project’s root analysis_options.yaml.
| Rule parameter | Default | Effect |
|---|---|---|
enabled |
true |
false switches the rule off for the whole project. |
exclude |
[] |
Path globs where the rule is silent. |
Suppressing it
Section titled “Suppressing it”Write why on the line directly above the suppression:
// The kind of the object is all the log needs.// ignore: assay/default_to_string// ignore_for_file: assay/default_to_string silences it in a whole file, and // ignore: default_to_string without the prefix works too. See Suppressions.