recursive_to_string
Reports a toString that makes text of the object itself, which calls the same toString again without end. Write the fields, or super.toString().
- Tier
- blocking
- Scope
- Dart
- Lowest preset
- recommended
- Fix
- none
What it reports
Section titled “What it reports”A place in the toString of a class, mixin or enum that makes text of the object itself:
'$this'or'${this}';this.toString(), ortoString()without a receiver;buffer.write(this)orwriteln(this)on aStringSinksuch as aStringBuffer.
The violation covers the expression.
It does not report:
- code in a closure in the body, which may run where the object is made text otherwise;
super.toString(), the inherited text;- the
toStringof an extension or extension type, wherethisis made text by thetoStringof the value it extends.
The object works fine until it is printed. Then the first log line, error report or debugger view that shows it overflows the stack, often far from the toString. It is most often a “show everything” toString that meant super.toString() or the fields.
Example
Section titled “Example”@overrideString toString() => 'Point($this)';@overrideString toString() => 'Point($x, $y)';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 test of the overflow needs it.// ignore: recursive_to_stringString toString() => '$this';// ignore_for_file: assay/recursive_to_string silences it in a whole file, and the prefixed // ignore: assay/recursive_to_string works too. See Suppressions.