unawaited_ return_ in_ try_ block
Details about the 'unawaited_return_in_try_block' diagnostic produced by the Dart analyzer.
Returning a 'Future' without 'await' inside a try block.
Description
#
The analyzer produces this diagnostic when a Future is returned from within
a try block, in an async function, without awaiting the returned
Future.
Example
#
The following code produces this diagnostic because the Future returned
from futureString is returned without using await. This can lead to
unexpected behavior because exceptions thrown by the Future won't be
caught by the catch block.
Future<String> futureString(Future<String> value) async {
try {
return value;
} catch (e) {
print(e);
return 'default';
}
}
Common fixes
#Add await before the returned Future:
Future<String> futureString(Future<String> value) async {
try {
return await value;
} catch (e) {
print(e);
return 'default';
}
}
Unless stated otherwise, the documentation on this site reflects Dart 3.12.2. Report an issue.