Reputation: 3716
my linter is telling me this await
is unnecessary:
Unnecessary await keyword in return.
I thought if you're calling a function inside an async function and you want to get/return the value, rather than the future you had to use await
to designate you want the value, not the future.
Am I missing something here?
Upvotes: 0
Views: 440
Reputation: 41008
As the documentation for that recommendation points out, you can take off the async
and just return the Future
, rather than using await
:
Future<Dictionary> get _localDir => getApplicationDocumentsDirectory();
This is called "eliding async/await". Stephen Cleary wrote an article about it (written for C#, but largely applicable for any language that uses async
and await
), which you may find helpful: Eliding Async and Await
In short, it's more efficient to do this in situations when you can:
By not including these keywords, the compiler can skip generating the async state machine. This means that there are fewer compiler-generated types in your assembly, less pressure on the garbage collector, and fewer CPU instructions to execute.
Upvotes: 1