avoid-future-tostring
preset: recommended
Warns when a Future
is the target of the toString
method or is used in an interpolation.
The result of calling toString()
on a Future
is Instance of '_Future<String>'
and is usually not expected.
Example
❌ Bad:
void main() {
final myFuture = Future.value(1);
// LINT: Avoid calling 'toString' on 'Futures'. Try awaiting the value before calling 'toString'.
print(myFuture.toString());
// LINT: Avoid calling 'toString' on 'Futures'. Try awaiting the value before calling 'toString'.
print('hello $myFuture');
}
✅ Good:
Future<void> withAwait() async {
final myFuture = Future.value(1);
print((await myFuture).toString()); // Correct, awaited
print('hello ${(await myFuture)}'); // Correct, awaited
}