MetaStack
MetaStack

Reputation: 3716

dart flutter - Why is this await unnecessary?

my linter is telling me this await is unnecessary: await

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

Answers (1)

Gabriel Luci
Gabriel Luci

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

Related Questions