Member-only story

FLUTTER INTERVIEW QUESTIONS

When to use async, await, then and Future in Dart?

Jelena Lecic
3 min readDec 20, 2019

--

When you’re new to Dart/Flutter this can get really confusing, but when you run few examples it does not look tricky anymore.

Async means that this function is asynchronous and you might need to wait a bit to get its result.

Await literally means - wait here until this function is finished and you will get its return value.

Future is a type that ‘comes from the future’ and returns value from your asynchronous function. It can complete with success(.then) or with
an error(.catchError).

.Then((value){…}) is a callback that’s called when future completes successfully(with a value).

void main() async {
print(getMeSomeFood());
print(await getMeSomethingBetter()); maybeSomethingSweet().then((String value) {
print(value);
});
}
Future<String> getMeSomeFood() async {
return "an…

--

--

Responses (10)