Skip to main content

avoid-recursive-tostring

Warns when a toString method calls itself recursively.

Recursive toString calls are never intentional and lead to runtime exceptions.

Example

❌ Bad:

class First {
// LINT: Avoid recursive 'toString' calls.
String toString() => 'Something $this';
}

class Second {
// LINT: Avoid recursive 'toString' calls.
String toString() => 'Something ${this}';
}

class Third {
// LINT: Avoid recursive 'toString' calls.
String toString() => 'Something ${this.toString()}';
}

class Forth {
// LINT: Avoid recursive 'toString' calls.
String toString() => 'Something ' + this.toString();
}

✅ Good:

class First {
String another() => 'hi';

String toString() => 'Something ${this.another()}';
}